home *** CD-ROM | disk | FTP | other *** search
/ CD Classic 39 / CD CLASSIC #39 (1998).iso / EMPRESA / visio / Vistdstd / Install / Data.Z / DVS.BAS < prev    next >
BASIC Source File  |  1997-06-26  |  91KB  |  2,362 lines

  1. Attribute VB_Name = "DVS"
  2. ' -----------------------------------------------------------------------------
  3. ' Copyright (C) 1997 Visio Corporation. All rights reserved.
  4. '
  5. ' You have a royalty-free right to use, modify, reproduce and distribute
  6. ' the Sample Application Files (and/or any modified version) in any way
  7. ' you find useful, provided that you agree that Visio has no warranty,
  8. ' obligations or liability for any Sample Application Files.
  9. ' -----------------------------------------------------------------------------
  10. Option Explicit
  11.  
  12. Global Const DVS_TITLE = "Developing Visio Solutions"
  13.  
  14. Sub AddGeometry()
  15. Attribute AddGeometry.VB_Description = "Adds a geometry section containing line rows to the first shape in the current selection."
  16. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes."
  17. ' This example demonstrates:
  18. '   retrieving an instance of Visio
  19. '   using an error handler
  20. '   retrieving the current selection collection
  21. '   accessing a shape from the selection collection
  22. '   adding a geometry section to a shape
  23. '   adding rows to a section
  24. '   setting a cell's formula
  25.  
  26. Dim appVisio As Visio.Application ' Visio instance
  27. Dim shpObj As Visio.Shape   ' shape object
  28. Dim i As Integer            ' Loop variable
  29. Dim iSection As Integer     ' holds section index for newly added section
  30.  
  31.     ' Set an error handler to catch the error if no shape is selected
  32.     On Error GoTo errNoShp
  33.     
  34.     ' Retrieve the current instance of Visio
  35.     Set appVisio = GetObject(, "visio.application")
  36.     
  37.     ' Retrive the first shape in the selection collection of the active window
  38.     ' The code below uses the more typical short form of the code
  39.     ' set shpObj = ActiveWindow.Selection.Item(1)
  40.     Set shpObj = appVisio.ActiveWindow.Selection(1)
  41.     
  42.     ' Reset the error handler - If we made it this far a shape is selected
  43.     On Error GoTo 0
  44.  
  45.     ' Add a geometry section to the shape
  46.     ' iSection gets the index for the new section
  47.     iSection = shpObj.AddSection(visSectionFirstComponent)
  48.  
  49.     ' Add the component row and the start row
  50.     shpObj.AddRow iSection, visRowFirst + 0, visTagComponent
  51.     shpObj.AddRow iSection, visRowVertex + 0, visTagMoveTo
  52.  
  53.     ' Add four LineTo rows. This creates the vertices
  54.     For i = 1 To 4
  55.         shpObj.AddRow iSection, visRowVertex + i, visTagLineTo
  56.     Next i
  57.  
  58.     ' Set formulas for the width and height cells in the new rows
  59.     ' This could be done in one call using SetFormulas. See SetTheFormulas()
  60.     ' for an example of SetFormulas
  61.     shpObj.CellsSRC(iSection, visRowVertex + 0, visX).Formula = "Width * 0.25"
  62.     shpObj.CellsSRC(iSection, visRowVertex + 0, visY).Formula = "Height * 0.5"
  63.     shpObj.CellsSRC(iSection, visRowVertex + 1, visX).Formula = "Width * 0.5"
  64.     shpObj.CellsSRC(iSection, visRowVertex + 1, visY).Formula = "Height * 0.25"
  65.     shpObj.CellsSRC(iSection, visRowVertex + 2, visX).Formula = "Width * 0.75"
  66.     shpObj.CellsSRC(iSection, visRowVertex + 2, visY).Formula = "Height * 0.5"
  67.     shpObj.CellsSRC(iSection, visRowVertex + 3, visX).Formula = "Width * 0.5"
  68.     shpObj.CellsSRC(iSection, visRowVertex + 3, visY).Formula = "Height * 0.75"
  69.     shpObj.CellsSRC(iSection, visRowVertex + 4, visX).Formula = "Geometry1.X1"
  70.     shpObj.CellsSRC(iSection, visRowVertex + 4, visY).Formula = "Geometry1.Y1"
  71.  
  72.     ' Exit the procedure bypassing the error handler
  73.     Exit Sub
  74.     
  75. errNoShp:
  76.     ' Display a message to let the user know that we detected that there
  77.     ' was no shape selected
  78.     MsgBox "Visio must be running and a shape selected!", vbOKOnly, DVS_TITLE
  79.     
  80. End Sub
  81.  
  82. Sub AddMenu()
  83. Attribute AddMenu.VB_Description = "Adds a Demo menu containing a menuitem that runs the ShowArgs demo program."
  84. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  85. 'This example demonstrates:
  86. '   retrieving an instance of Visio
  87. '   retrieving a copy of Visio's built-in menus
  88. '   adding a Demo menu to before the Window menu
  89. '   adding a menu item to the new menu (Show Args)
  90.     
  91. Dim appVisio As Visio.Application ' Visio instance
  92. Dim uiObj As Visio.UIObject
  93. Dim menuSetObj As Visio.MenuSet
  94. Dim menuObj As Visio.Menu
  95. Dim menuItemObj As Visio.MenuItem
  96.  
  97.     ' Set an error handler
  98.     On Error GoTo errHandler
  99.     
  100.     ' Retrieve the current instance of Visio
  101.     Set appVisio = GetObject(, "Visio.Application")
  102.     
  103.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  104.     Set uiObj = appVisio.BuiltInMenus
  105.  
  106.     ' Set menuSetObj to the Drawing menu set
  107.     Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)
  108.     
  109.     ' Add a new menu at ordinal position 7
  110.     ' Since we retrieved the builtin menus, we know that this will add the menu
  111.     ' before the Window menu. If we had retrieved a custom UI, we would have to
  112.     ' loop through the menus checking the caption to find the Window menu. In a
  113.     ' custom menu there is no guarantee that we would find a Window menu since it
  114.     ' could be deleted. After running this example, the ordinal position of the
  115.     ' Demo menu is 7 and the Window menu shifts to ordinal position 8.
  116.     Set menuObj = menuSetObj.Menus.AddAt(7)
  117.  
  118.     ' Set the caption for the new menu
  119.     menuObj.Caption = "Demo"
  120.  
  121.     ' Add a menuitem to the new menu
  122.     Set menuItemObj = menuObj.MenuItems.Add
  123.  
  124.     ' Set the properties for the new menuitem
  125.     menuItemObj.Caption = "Run &ShowArgs"
  126.     menuItemObj.AddOnName = "ShowArgs.EXE"
  127.     menuItemObj.AddOnArgs = "/DVS=Fun"
  128.     menuItemObj.ActionText = "Run ShowArgs"
  129.     menuItemObj.MiniHelp = "Run the ShowArgs application"
  130.  
  131.     ' Tell Visio to use the new UI for this document
  132.     appVisio.ActiveDocument.SetCustomMenus uiObj
  133.     
  134.     ' Display a message box telling the user how to undo this change
  135.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  136.         vbInformation, DVS_TITLE
  137.     
  138.     ' Exit the procedure bypassing the error handler
  139.     Exit Sub
  140.     
  141. errHandler:
  142.     ' Display a message to let the user know that we detected that
  143.     ' Visio was not running
  144.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  145.     
  146. End Sub
  147.  
  148. Sub AddMenuItem()
  149. Attribute AddMenuItem.VB_Description = "Adds a menuitem that runs the ShowArgs demo program to the Tools menu."
  150. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  151. 'This example demonstrates:
  152. '   retrieving an instance of Visio
  153. '   retrieving a copy of Visio's built-in menus
  154. '   retrieving an existing menu from the menuset (Tools)
  155. '   adding a menu item to the menu (Run ShowArgs)
  156. '   setting the custom menu for use by Visio
  157.     
  158. Dim appVisio As Visio.Application ' Visio instance
  159. Dim uiObj As Visio.UIObject
  160. Dim menuSetObj As Visio.MenuSet
  161. Dim menuObj As Visio.Menu
  162. Dim menuItemObj As Visio.MenuItem
  163.  
  164.     ' Set an error handler
  165.     On Error GoTo errHandler
  166.     
  167.     ' Retrieve the current instance of Visio
  168.     Set appVisio = GetObject(, "visio.application")
  169.      
  170.      ' Retrieve the UIObject for the copy of the BuiltInMenus
  171.     Set uiObj = appVisio.BuiltInMenus
  172.  
  173.     ' Set menuSetObj to the Drawing menu set
  174.     Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)
  175.     
  176.     ' retrieve the Tools menu
  177.     ' Since we started from the BuiltInMenus we can use the position
  178.     ' number. If we had started from a custom menu, we would need to locate
  179.     ' the menu by searching through all the menus and would need to handle
  180.     ' the case of not finding the menu (since it could be deleted in a custom
  181.     ' menu)
  182.     Set menuObj = menuSetObj.Menus(5)
  183.  
  184.     ' Add a menuitem to the start of the Tools menu
  185.     Set menuItemObj = menuObj.MenuItems.AddAt(0)
  186.  
  187.     ' Set the properties for the new menuitem
  188.     menuItemObj.Caption = "Run &ShowArgs"
  189.     menuItemObj.AddOnName = "ShowArgs.EXE"
  190.     menuItemObj.AddOnArgs = "/DVS=Fun"
  191.     menuItemObj.ActionText = "Run ShowArgs"
  192.     menuItemObj.MiniHelp = "Run the ShowArgs application"
  193.  
  194.     ' Tell Visio to use the new UI
  195.     appVisio.ActiveDocument.SetCustomMenus uiObj
  196.     
  197.     ' Display a message box telling the user how to undo this change
  198.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  199.         vbInformation, DVS_TITLE
  200.     
  201.     ' Exit the procedure bypassing the error handler
  202.     Exit Sub
  203.     
  204. errHandler:
  205.     ' Display a message to let the user know that we detected that
  206.     ' Visio was not running
  207.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  208.  
  209. End Sub
  210.  
  211. Sub AddShortcutMenuItem()
  212. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  213. 'This example demonstrates:
  214. '   retrieving an instance of Visio
  215. '   retrieving a copy of Visio's built-in menus
  216. '   retrieving the drawing object selected context menu set
  217. '   adding a menu item to the menu (Show ShapeSheet)
  218. '   setting the custom menu for use by Visio
  219.     
  220. Dim appVisio As Visio.Application ' Visio instance
  221. Dim uiObj As Visio.UIObject
  222. Dim menuSetObj As Visio.MenuSet
  223. Dim menuObj As Visio.Menu
  224. Dim menuItemObj As Visio.MenuItem
  225.     
  226.     ' Set an error handler
  227.     On Error GoTo errHandler
  228.     
  229.     ' Retrieve the current instance of Visio
  230.     Set appVisio = GetObject(, "visio.application")
  231.  
  232.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  233.     Set uiObj = appVisio.BuiltInMenus
  234.  
  235.     ' Set menuSetObj to the Drawing Object Context menu set
  236.     Set menuSetObj = uiObj.MenuSets.ItemAtID(Visio.visUIObjSetCntx_DrawObjSel)
  237.     
  238.     ' retrieve the menu
  239.     Set menuObj = menuSetObj.Menus(0)
  240.  
  241.     ' Add a menuitem to the start of the menu
  242.     Set menuItemObj = menuObj.MenuItems.AddAt(0)
  243.  
  244.     ' Set the properties for the new menuitem
  245.     menuItemObj.Caption = "Show ShapeSheet"
  246.     menuItemObj.CmdNum = Visio.visCmdWindowShowShapeSheet
  247.     
  248.     ' Add a divider bar
  249.     Set menuItemObj = menuObj.MenuItems.AddAt(1)
  250.  
  251.     ' Tell Visio to use the new UI
  252.     appVisio.ActiveDocument.SetCustomMenus uiObj
  253.     
  254.     ' Display a message box telling the user how to undo this change
  255.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  256.         vbInformation, DVS_TITLE
  257.     
  258.     ' Exit the procedure bypassing the error handler
  259.     Exit Sub
  260.     
  261. errHandler:
  262.     ' Display a message to let the user know that we detected that
  263.     ' Visio was not running
  264.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  265.  
  266. End Sub
  267.  
  268. Sub AddToolbarButton()
  269. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  270. 'This example demonstrates:
  271. '   retrieving an instance of Visio
  272. '   retrieving a copy of Visio's built-in toolbars (MS Office flavor)
  273. '   retrieving a specific toolbarset (Drawing toolbarset)
  274. '   adding a toolbar button (DVS button that runs the Chart Shape Wizard)
  275. '   setting the custom toolbar for use by Visio
  276.     
  277. Dim appVisio As Visio.Application ' Visio instance
  278. Dim uiObj As Visio.UIObject
  279. Dim toolbarSetObj As Visio.ToolbarSet
  280. Dim toolbarItemsObj As Visio.ToolbarItems
  281. Dim objNewToolbarItem As Visio.ToolbarItem
  282.  
  283.     ' Set an error handler
  284.     On Error GoTo errHandler
  285.     
  286.     ' Retrieve the current instance of Visio
  287.     Set appVisio = GetObject(, "visio.application")
  288.  
  289.     ' Get the UI object for the copy of the MSOffice toolbars
  290.     Set uiObj = appVisio.BuiltInToolbars(visToolBarMSOffice)
  291.  
  292.     ' Get the Drawing Window toolbarsets
  293.     ' NOTE: You must use ItemAtID to get the toolbarset
  294.     ' using uiObj.ToolbarSets(visUIObjSetDrawing) will not work
  295.     Set toolbarSetObj = uiObj.ToolbarSets.ItemAtID(visUIObjSetDrawing)
  296.  
  297.     ' Get the ToolbarItems collection
  298.     Set toolbarItemsObj = toolbarSetObj.Toolbars(0).ToolbarItems
  299.  
  300.     ' Add a new button in the first position
  301.     Set objNewToolbarItem = toolbarItemsObj.AddAt(0)
  302.  
  303.     ' set the properties for the new toolbar button
  304.     objNewToolbarItem.ActionText = "Run Chart Shape Wizard"
  305.     objNewToolbarItem.AddOnName = "Chart Shape Wizard.exe"
  306.     objNewToolbarItem.CntrlType = visCtrlTypeBUTTON
  307.     objNewToolbarItem.Priority = 1
  308.     
  309.     ' set the icon
  310.     objNewToolbarItem.IconFileName "dvs.ico"
  311.  
  312.     ' Tell Visio to actually use the new custom UI
  313.     appVisio.ActiveDocument.SetCustomToolbars uiObj
  314.  
  315.     ' Display a message box telling the user how to undo this change
  316.     MsgBox "Custom toolbar set. Run the RestoreBuiltInUI macro to clear the custom toolbar.", _
  317.         vbInformation, DVS_TITLE
  318.     
  319.     ' Exit the procedure bypassing the error handler
  320.     Exit Sub
  321.     
  322. errHandler:
  323.     ' Display a message to let the user know that we detected that
  324.     ' Visio was not running
  325.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  326.  
  327. End Sub
  328.  
  329. Sub ChangeToolbarButtonIcon()
  330. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  331. 'This example demonstrates:
  332. '   retrieving an instance of Visio
  333. '   retrieving a copy of Visio's built-in toolbars (MSOffice flavor)
  334. '   retrieving a specific toolbarset (Drawing)
  335. '   retrieving a specific toolbaritem (TurnToNextPage)
  336. '   modifying the icon on a toolbaritem (DVS icon)
  337. '   setting the custom toolbar for use by Visio
  338.  
  339. Dim appVisio As Visio.Application ' Visio instance
  340. Dim uiObj As Visio.UIObject
  341. Dim toolbarSetObj As Visio.ToolbarSet
  342. Dim toolbarItemsObj As Visio.ToolbarItems
  343. Dim toolbarItemObj As Visio.ToolbarItem
  344. Dim i As Integer                            ' Loop variable
  345. Dim bFound As Boolean                       ' Found flag
  346.     
  347.     ' Set an error handler
  348.     On Error GoTo errHandler
  349.     
  350.     ' Retrieve the current instance of Visio
  351.     Set appVisio = GetObject(, "visio.application")
  352.  
  353.     ' Get the UI object for the copy of the MSOffice toolbars
  354.     Set uiObj = appVisio.BuiltInToolbars(visToolBarMSOffice)
  355.  
  356.     ' Get the Drawing Window toolbarsets
  357.     ' NOTE: You must use ItemAtID to get the toolbarset
  358.     ' using uiObj.ToolbarSets(visUIObjSetDrawing) will not work
  359.     Set toolbarSetObj = uiObj.ToolbarSets.ItemAtID(visUIObjSetDrawing)
  360.  
  361.     ' Get the ToolbarItems collection
  362.     Set toolbarItemsObj = toolbarSetObj.Toolbars(4).ToolbarItems
  363.  
  364.     ' Get the ToolbarItem for the NextPage toolbarbutton
  365.     bFound = False
  366.     For i = 0 To toolbarItemsObj.Count - 1
  367.         Set toolbarItemObj = toolbarItemsObj(i)
  368.         If toolbarItemObj.CmdNum = Visio.visCmdTurnToNextPage Then
  369.             bFound = True
  370.             Exit For
  371.         End If
  372.     Next i
  373.  
  374.     If bFound Then
  375.         ' set the icon to our DVS icon
  376.         toolbarItemObj.IconFileName "dvs.ico"
  377.         
  378.         ' Tell Visio to actually use the new custom UI
  379.         appVisio.ActiveDocument.SetCustomToolbars uiObj
  380.     
  381.         ' Display a message box telling the user how to undo this change
  382.         MsgBox "Custom toolbar set. Run the RestoreBuiltInUI macro to clear the custom toolbar.", _
  383.             vbInformation, DVS_TITLE
  384.     Else
  385.         ' Let the user know we couldn't find the toolbarbutton
  386.         ' This shouldn't happen since were starting from the builtin toolbars
  387.         MsgBox "Unable to locate toolbar button", vbOKOnly, DVS_TITLE
  388.     End If
  389.     
  390.     ' Exit the procedure bypassing the error handler
  391.     Exit Sub
  392.     
  393. errHandler:
  394.     ' Display a message to let the user know that we detected that
  395.     ' Visio was not running
  396.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  397.     
  398.  
  399. End Sub
  400.  
  401. Sub ChangeToolbarButtonPriority()
  402. Attribute ChangeToolbarButtonPriority.VB_Description = "Changes the priority of the TurnToNextPage button so that it will have a lower probability of being removed fom the toolbar."
  403. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  404. 'This example demonstrates:
  405. '   retrieving an instance of Visio
  406. '   retrieving a copy of Visio's built-in toolbars
  407. '   retrieving a specific toolbarset
  408. '   retrieving a specific toolbaritem (TurnToNextPage)
  409. '   modifying the priority of a toolbaritem
  410. '   setting the custom toolbar for use by Visio
  411.  
  412. Dim appVisio As Visio.Application ' Visio instance
  413. Dim uiObj As Visio.UIObject
  414. Dim toolbarSetObj As Visio.ToolbarSet
  415. Dim toolbarItemsObj As Visio.ToolbarItems
  416. Dim toolbarItemObj As Visio.ToolbarItem
  417. Dim i As Integer                            ' Loop variable
  418. Dim bFound As Boolean                       ' Found flag
  419.     
  420.     ' Set an error handler
  421.     On Error GoTo errHandler
  422.     
  423.     ' Retrieve the current instance of Visio
  424.     Set appVisio = GetObject(, "visio.application")
  425.  
  426.     ' Get the UI object for the copy of the MSOffice toolbars
  427.     Set uiObj = appVisio.BuiltInToolbars(visToolBarMSOffice)
  428.  
  429.     ' Get the Drawing Window toolbarsets
  430.     ' NOTE: You must use ItemAtID to get the toolbarset
  431.     ' using uiObj.ToolbarSets(visUIObjSetDrawing) will not work
  432.     Set toolbarSetObj = uiObj.ToolbarSets.ItemAtID(visUIObjSetDrawing)
  433.  
  434.     ' Get the ToolbarItems collection
  435.     Set toolbarItemsObj = toolbarSetObj.Toolbars(4).ToolbarItems
  436.  
  437.     ' Get the ToolbarItem for the NextPage toolbarbutton
  438.     ' While we know that it will be found since we retrieved the builtin toolbars,
  439.     ' this example demonstrates a technique that can be used when a custom toolbar
  440.     ' is being searched.
  441.     bFound = False
  442.     For i = 0 To toolbarItemsObj.Count - 1
  443.         Set toolbarItemObj = toolbarItemsObj(i)
  444.         If toolbarItemObj.CmdNum = Visio.visCmdTurnToNextPage Then
  445.             bFound = True
  446.             Exit For
  447.         End If
  448.     Next i
  449.  
  450.     If bFound Then
  451.         ' set the priority to 1 (highest)
  452.         toolbarItemObj.Priority = 1
  453.         
  454.         ' Tell Visio to actually use the new custom UI
  455.         appVisio.ActiveDocument.SetCustomToolbars uiObj
  456.     
  457.         ' Display a message box telling the user how to undo this change
  458.         MsgBox "Custom toolbar set. Run the RestoreBuiltInUI macro to clear the custom toolbar.", _
  459.             vbInformation, DVS_TITLE
  460.     Else
  461.         ' Let the user know we couldn't find the toolbarbutton
  462.         ' This shouldn't happen since were starting from the builtin toolbars
  463.         MsgBox "Unable to locate toolbar button", vbOKOnly, DVS_TITLE
  464.     End If
  465.     
  466.     ' Exit the procedure bypassing the error handler
  467.     Exit Sub
  468.     
  469. errHandler:
  470.     ' Display a message to let the user know that we detected that
  471.     ' Visio was not running
  472.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  473.     
  474. End Sub
  475.  
  476. Sub DeleteAccelItem()
  477. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  478. 'This example demonstrates:
  479. '   retrieving an instance of Visio
  480. '   retrieving a copy of Visio's built-in menus
  481. '   retrieving a specific menuset (Drawing)
  482. '   retrieving a specific menu (Format)
  483. '   deleting the menu
  484. '   setting the custom menu for use by Visio
  485.  
  486. Dim appVisio As Visio.Application ' Visio instance
  487. Dim uiObj As Visio.UIObject
  488. Dim accelTableObj As Visio.AccelTable
  489. Dim accelItemsObj As Visio.AccelItems
  490. Dim accelItemObj As Visio.AccelItem
  491. Dim i As Integer
  492.     
  493.     ' Set an error handler
  494.     On Error GoTo errHandler
  495.     
  496.     ' Retrieve the current instance of Visio
  497.     Set appVisio = GetObject(, "visio.application")
  498.  
  499.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  500.     Set uiObj = appVisio.BuiltInMenus
  501.  
  502.     ' Set accelTableObj to the Drawing menu set
  503.     Set accelTableObj = uiObj.AccelTables.ItemAtID(visUIObjSetDrawing)
  504.     
  505.     ' retrieve the accelerator items collections
  506.     Set accelItemsObj = accelTableObj.AccelItems
  507.     
  508.     ' retrieve the accelerator item for the Visual Basic Editor
  509.     ' To do this we must iterate through the collection and locate the item
  510.     ' of interest. The item can be identified either by checking the CmdNum for
  511.     ' the number we're interested in or by checking for the specific key. Since
  512.     ' checking for the key requires looking at the Alt, Control, Shift, and Key
  513.     ' properties it is better to use the CmdNum.
  514.     ' Since we retrieved the builtin menus, we know that we can find the accelerator.
  515.     ' If we had started from a custom menu, we would need to handle the case of
  516.     ' not finding the accelerator.
  517.     For i = 0 To accelItemsObj.Count - 1
  518.         Set accelItemObj = accelItemsObj.Item(i)
  519.         If accelItemObj.CmdNum = Visio.visCmdToolsRunVBE Then
  520.             Exit For
  521.         End If
  522.     Next i
  523.     
  524.     ' delete the accelerator
  525.     accelItemObj.Delete
  526.     
  527.     ' Tell Visio to use the new UI
  528.     appVisio.ActiveDocument.SetCustomMenus uiObj
  529.     
  530.     ' Display a message box telling the user how to undo this change
  531.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  532.         vbInformation, DVS_TITLE
  533.     
  534.     ' Exit the procedure bypassing the error handler
  535.     Exit Sub
  536.     
  537. errHandler:
  538.     ' Display a message to let the user know that we detected that
  539.     ' Visio was not running
  540.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  541.     
  542. End Sub
  543.  
  544. Sub DeleteHierarchicalMenuItem()
  545. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  546. 'This example demonstrates:
  547. '   retrieving an instance of Visio
  548. '   retrieving a copy of Visio's built-in menus
  549. '   retrieving a specific menuset (Drawing)
  550. '   retrieving a specific menu (Tools)
  551. '   retrieving a specific menuitem from the menu (Macro)
  552. '   retrieving the menuitems collection for the hierarchical menu
  553. '   retrieving a specific menuitem from the menuitems collection (Run VBE)
  554. '   deleting the menuitem
  555. '   setting the custom menu for use by Visio
  556.  
  557. Dim appVisio As Visio.Application ' Visio instance
  558. Dim uiObj As Visio.UIObject         ' BuiltInMenus UIObject
  559. Dim menuSetObj As Visio.MenuSet     ' Drawing menuset
  560. Dim menuObj As Visio.Menu           ' Window menu
  561. Dim menuItemsObj As Visio.MenuItems  ' Window menu MenuItems collection
  562. Dim menuItemObj As Visio.MenuItem   ' MenuItem (ultimately Macro)
  563. Dim hiermenuItemsObj As Visio.MenuItems ' Macro hierarchical MenuItems collection
  564. Dim hiermenuItemObj As Visio.MenuItem   ' Macro hierarchical MenuItem (Run VBE)
  565. Dim i, j As Integer                   ' Loop variables
  566.     
  567.     ' Set an error handler
  568.     On Error GoTo errHandler
  569.     
  570.     ' Retrieve the current instance of Visio
  571.     Set appVisio = GetObject(, "visio.application")
  572.  
  573.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  574.     Set uiObj = appVisio.BuiltInMenus
  575.  
  576.     ' Set menuSetObj to the Drawing menu set
  577.     Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)
  578.     
  579.     ' retrieve the Tools menu
  580.     ' Since we retrieved the builtin menus, we know that we can find the Tools menu
  581.     ' by its position. If we had retrieved a custom UI, we would have to loop through
  582.     ' the menus checking the caption to find the Tools menu. In a custom menu there
  583.     ' is no guarantee that we would find a Tools menu since it could be deleted.
  584.     Set menuObj = menuSetObj.Menus(5)
  585.     
  586.     ' retrieve the MenuItems collection for the Tools menu
  587.     Set menuItemsObj = menuObj.MenuItems
  588.     
  589.     ' locate the Macro menuitem
  590.     ' Since we retrieved the builtin menus we know we will find it. If we had
  591.     ' started from a custom menu we would need to handle the case of not finding
  592.     ' the menuitem.
  593.     For i = 0 To menuItemsObj.Count - 1
  594.         ' retrieve the current menuitem from the collection
  595.         Set menuItemObj = menuItemsObj(i)
  596.         
  597.         ' check the CmdNum to see if it is the Macro... command
  598.         If menuItemObj.CmdNum = Visio.visCmdHierarchical And _
  599.            menuItemObj.Caption = "&Macro" Then
  600.            
  601.             ' retrieve the menuitems collection for the hierarchical menu
  602.             Set hiermenuItemsObj = menuItemObj.MenuItems
  603.             
  604.             ' locate the Visual Basic Editor menuitem
  605.             ' As with the Macro menuitem, we know we will find the VBE menuitem
  606.             ' since we started with a copy of the builtin menus.
  607.             For j = 0 To hiermenuItemsObj.Count - 1
  608.                 ' retrieve menuitem from collection
  609.                 Set hiermenuItemObj = hiermenuItemsObj(j)
  610.                 
  611.                 ' check the CmdNum to see if it is Run VBE
  612.                 If hiermenuItemObj.CmdNum = Visio.visCmdToolsRunVBE Then
  613.                     ' delete the Visual Basic Editor menuitem
  614.                     ' Note: since the list of macros/add-ons is built keying off of this item
  615.                     ' the menuitems for all the add-ons and macros that are normally seen
  616.                     ' under the Visual Basic Editor menuitem will also disappear
  617.                     hiermenuItemObj.Delete
  618.                     
  619.                     ' Exit the inside for loop
  620.                     Exit For
  621.                 End If
  622.             Next j
  623.             
  624.             ' Exit the outer for loop
  625.             Exit For
  626.         End If
  627.     Next i
  628.     
  629.     ' Tell Visio to use the new UI
  630.     appVisio.ActiveDocument.SetCustomMenus uiObj
  631.     
  632.     ' Display a message box telling the user how to undo this change
  633.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  634.         vbInformation, DVS_TITLE
  635.  
  636.     ' Exit the procedure bypassing the error handler
  637.     Exit Sub
  638.     
  639. errHandler:
  640.     ' Display a message to let the user know that we detected that
  641.     ' Visio was not running
  642.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  643.     
  644. End Sub
  645.  
  646. Sub DeleteMenu()
  647. Attribute DeleteMenu.VB_Description = "Deletes the Format menu from the Drawing menuset."
  648. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  649. 'This example demonstrates:
  650. '   retrieving an instance of Visio
  651. '   retrieving a copy of Visio's built-in menus
  652. '   retrieving a specific menuset (Drawing)
  653. '   retrieving a specific menu (Format)
  654. '   deleting the menu
  655. '   setting the custom menu for use by Visio
  656.  
  657. Dim appVisio As Visio.Application ' Visio instance
  658. Dim uiObj As Visio.UIObject
  659. Dim menuSetObj As Visio.MenuSet
  660. Dim menuObj As Visio.Menu
  661.     
  662.     ' Set an error handler
  663.     On Error GoTo errHandler
  664.     
  665.     ' Retrieve the current instance of Visio
  666.     Set appVisio = GetObject(, "visio.application")
  667.  
  668.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  669.     Set uiObj = appVisio.BuiltInMenus
  670.  
  671.     ' Set menuSetObj to the Drawing menu set
  672.     Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)
  673.     
  674.     ' retrieve the Format menu
  675.     ' Since we retrieved the builtin menus, we know that we can find the format menu
  676.     ' by its position. If we had retrieved a custom UI, we would have to loop through
  677.     ' the menus checking the caption to find the Format menu. In a custom menu there
  678.     ' is no guarantee that we would find a Format menu since it could be deleted.
  679.     Set menuObj = menuSetObj.Menus(4)
  680.     
  681.     ' delete the Format menu
  682.     menuObj.Delete
  683.     
  684.     ' Tell Visio to use the new UI
  685.     appVisio.ActiveDocument.SetCustomMenus uiObj
  686.     
  687.     ' Display a message box telling the user how to undo this change
  688.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  689.         vbInformation, DVS_TITLE
  690.  
  691.     ' Exit the procedure bypassing the error handler
  692.     Exit Sub
  693.     
  694. errHandler:
  695.     ' Display a message to let the user know that we detected that
  696.     ' Visio was not running
  697.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  698.     
  699. End Sub
  700.  
  701. Sub DeleteMenuItem()
  702. Attribute DeleteMenuItem.VB_Description = "Deletes the ShowShapeSheet menuitem from the Drawing Window menu."
  703. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  704. 'This example demonstrates:
  705. '   retrieving an instance of Visio
  706. '   retrieving a copy of Visio's built-in menus
  707. '   retrieving a specific menuset (Drawing)
  708. '   retrieving a specific menu (Window)
  709. '   retrieving a specific menuitem from the menu (Show ShapeSheet)
  710. '   deleting the menuitem
  711. '   setting the custom menu for use by Visio
  712.  
  713. Dim appVisio As Visio.Application ' Visio instance
  714. Dim uiObj As Visio.UIObject         ' BuiltInMenus UIObject
  715. Dim menuSetObj As Visio.MenuSet     ' Drawing menuset
  716. Dim menuObj As Visio.Menu           ' Window menu
  717. Dim menuItemsObj As Visio.MenuItems  ' Window menu MenuItems collection
  718. Dim menuItemObj As Visio.MenuItem   ' MenuItem (ultimately Show ShapeSheet)
  719. Dim i As Integer                    ' Loop variable
  720.     
  721.     ' Set an error handler
  722.     On Error GoTo errHandler
  723.     
  724.     ' Retrieve the current instance of Visio
  725.     Set appVisio = GetObject(, "visio.application")
  726.  
  727.     ' Retrieve the UIObject for the copy of the BuiltInMenus
  728.     Set uiObj = appVisio.BuiltInMenus
  729.  
  730.     ' Set menuSetObj to the Drawing menu set
  731.     Set menuSetObj = uiObj.MenuSets.ItemAtID(visUIObjSetDrawing)
  732.     
  733.     ' retrieve the Window menu
  734.     ' Since we retrieved the builtin menus, we know that we can find the window menu
  735.     ' by its position. If we had retrieved a custom UI, we would have to loop through
  736.     ' the menus checking the caption to find the Window menu. In a custom menu there
  737.     ' is no guarantee that we would find a Window menu since it could be deleted.
  738.     Set menuObj = menuSetObj.Menus(7)
  739.     
  740.     ' retrieve the MenuItems collection for the Window menu
  741.     Set menuItemsObj = menuObj.MenuItems
  742.     
  743.     ' locate the Show ShapeSheet menuitem
  744.     ' Since we retrieved the builtin menus we know we will find it. If we had
  745.     ' started from a custom menu we would need to handle the case of not finding
  746.     ' the menuitem.
  747.     For i = 0 To menuItemsObj.Count - 1
  748.         ' retrieve the current menuitem from the collection
  749.         Set menuItemObj = menuItemsObj(i)
  750.         
  751.         ' check the CmdNum to see if it is Show ShapeSheet
  752.         If menuItemObj.CmdNum = Visio.visCmdWindowShowShapeSheet Then
  753.             ' delete the Show ShapeSheet menuitem
  754.             menuItemObj.Delete
  755.            Exit For
  756.         End If
  757.     Next i
  758.     
  759.     ' Tell Visio to use the new UI
  760.     appVisio.ActiveDocument.SetCustomMenus uiObj
  761.     
  762.     ' Display a message box telling the user how to undo this change
  763.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom menu.", _
  764.         vbInformation, DVS_TITLE
  765.  
  766.     ' Exit the procedure bypassing the error handler
  767.     Exit Sub
  768.     
  769. errHandler:
  770.     ' Display a message to let the user know that we detected that
  771.     ' Visio was not running
  772.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  773.     
  774. End Sub
  775.  
  776. Sub DeleteToolbarButton()
  777. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  778. 'This example demonstrates:
  779. '   retrieving an instance of Visio
  780. '   retrieving a copy of Visio's built-in toolbars
  781. '   retrieving a specific toolbarset
  782. '   deleting a toolbar button   (the spelling button)
  783. '   setting the custom toolbar for use by Visio
  784.     
  785. Dim appVisio As Visio.Application ' Visio instance
  786. Dim uiObj As Visio.UIObject
  787. Dim toolbarSetObj As Visio.ToolbarSet
  788. Dim toolbarItemsObj As Visio.ToolbarItems
  789. Dim toolbarItemObj As Visio.ToolbarItem
  790. Dim i As Integer                            ' Loop variable
  791.     
  792.     ' Set an error handler
  793.     On Error GoTo errHandler
  794.     
  795.     ' Retrieve the current instance of Visio
  796.     Set appVisio = GetObject(, "visio.application")
  797.  
  798.     ' Get the UI object for the copy of the MSOffice toolbars
  799.     Set uiObj = appVisio.BuiltInToolbars(visToolBarMSOffice)
  800.  
  801.     ' Get the Drawing Window toolbarsets
  802.     ' NOTE: You must use ItemAtID to get the toolbarset
  803.     ' using uiObj.ToolbarSets(visUIObjSetDrawing) will not work
  804.     Set toolbarSetObj = uiObj.ToolbarSets.ItemAtID(visUIObjSetDrawing)
  805.  
  806.     ' Get the ToolbarItems collection
  807.     Set toolbarItemsObj = toolbarSetObj.Toolbars(0).ToolbarItems
  808.  
  809.     ' retrieve the spelling toolbaritem
  810.     ' Since we retrieved the builtin toolbars we know that we will find it.
  811.     ' If we had started from a custom toolbar we would need to handle the case
  812.     ' of not finding the button.
  813.     For i = 0 To toolbarItemsObj.Count - 1
  814.         ' retrieve the current toolbaritem from the collection
  815.         Set toolbarItemObj = toolbarItemsObj(i)
  816.         
  817.         ' check whether the current item is the spelling button
  818.         If toolbarItemObj.CmdNum = Visio.visCmdToolsSpelling Then
  819.             Exit For
  820.         End If
  821.     Next i
  822.     
  823.     ' delete the spell check button
  824.     toolbarItemObj.Delete
  825.  
  826.     ' Tell Visio to actually use the new custom UI
  827.     appVisio.ActiveDocument.SetCustomToolbars uiObj
  828.  
  829.     ' Display a message box telling the user how to undo this change
  830.     MsgBox "Custom toolbar set. Run the RestoreBuiltInUI macro to clear the custom toolbar.", _
  831.         vbInformation, DVS_TITLE
  832.  
  833.     ' Exit the procedure bypassing the error handler
  834.     Exit Sub
  835.     
  836. errHandler:
  837.     ' Display a message to let the user know that we detected that
  838.     ' Visio was not running
  839.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  840.     
  841. End Sub
  842.  
  843. Sub DropManyShapes()
  844. Attribute DropManyShapes.VB_Description = "Demonstrates using the DropMany method to add multiple shapes to a drawing with one call."
  845. ' Developing Visio Solutions, Chapter 12, "Creating Visio drawings from a program"
  846. ' This example demonstrates:
  847. '   retrieving an instance of Visio
  848. '   retrieving the local masters collection for appVisio.ActiveDocument
  849. '   retrieving the masters collection for a document
  850. '   droping multiple masters in one call with DropMany
  851.  
  852. Dim appVisio As Visio.Application ' Visio instance
  853. Dim pagObj As Visio.Page        ' Page to work in
  854. Dim pagsObj As Visio.Pages      ' Pages collection of document
  855. Dim mastersObj As Visio.Masters ' Master collection of stencil
  856. Dim nCnt As Integer             ' Number of masters on stencil
  857. Dim nDropped As Integer         ' Number of shapes dropped
  858. Dim i As Integer                ' Loop variable
  859.  
  860. Dim iRunMode As Integer         ' DropMany mode to Demo
  861. Const BYOBJECT = 0              '   pass master interface
  862. Const BYINDEX = 1               '   pass index into masters collection
  863. Const BYNAME = 2                '   pass master name
  864. Const BYUNIQUEID = 3            '   pass unique id
  865.  
  866.     ' Set an error handler
  867.     On Error GoTo errHandler
  868.     
  869.     ' Initialize the desired demo run mode
  870.     iRunMode = BYUNIQUEID
  871.     
  872.     ' Retrieve the current instance of Visio
  873.     Set appVisio = GetObject(, "visio.application")
  874.     
  875.     ' Get the pages collection for the document
  876.     ' Note the use of appVisio.ActiveDocument to refer to the current document
  877.     Set pagsObj = appVisio.ActiveDocument.Pages
  878.     
  879.     ' Get a reference to the first page of the collection
  880.     ' The code below uses the more typical short form of the code
  881.     ' Set pagObj = pagsObj.Item(1)
  882.     Set pagObj = pagsObj(1)
  883.  
  884.     ' Retrieve the masters collection for the local master stencil
  885.     Set mastersObj = appVisio.ActiveDocument.Masters
  886.     
  887.     ' Get the size of the masters collection
  888.     nCnt = mastersObj.Count
  889.     
  890.     ' Tell the user how to use this example if no shapes are currently in
  891.     ' the local master stencil
  892.     If nCnt = 0 Then
  893.         MsgBox "This example requires shapes on the local master stencil." & _
  894.             vbCrLf & vbCrLf & _
  895.             "Drop shapes from a stencil onto the drawing page to add them" & _
  896.             vbCrLf & "to the local master stencil.", _
  897.             vbOKOnly, DVS_TITLE
  898.         Exit Sub
  899.     End If
  900.  
  901.     ' Setup the arrays required by DropMany
  902.     ' DropMany takes 3 parameters
  903.     '   An array of variants for the master(s)
  904.     '   An array of doubles for the x,y drop coordinates
  905.     '   An integer array DropMany fills with the shape ids for the dropped shapes
  906.     '
  907.     ' The masters array can be an array of master objects, master names,
  908.     ' master indices, or unique ids.
  909.     '
  910.     ' If less master elements are given than coordinates, the last specified
  911.     ' master is dropped for the remaining coordinates. If you want to drop n
  912.     ' copies of one master and m of another you need a masters array of n+1
  913.     ' elements. The first element should be a reference to the first master,
  914.     ' the next n-1 elements should be left unitialized variants (VT_EMPTY),
  915.     ' the last element should be a reference to the second master
  916.     
  917.     ' Masters array             Coordinates
  918.     ' |------------|            |-------|
  919.     ' | Master ref |            | x, y  |
  920.     ' | VT_EMPTY   |            | x, y  |
  921.     ' |     .      |            | x, y  |
  922.     ' |     .      |            | x, y  |
  923.     ' |     .      |            | x, y  |
  924.     ' | VT_EMPTY   |            | x, y  |
  925.     ' | Master ref |            | x, y  |
  926.     ' |------------|            |-------|
  927.  
  928.     ' Note if you use a Dim statement like Dim marray(6) remember that depending
  929.     ' on the Option Base setting this is either 7 elements (by default) or
  930.     ' 6 (if you explicitly set option base 1)
  931.     ReDim marray(1 To nCnt) As Variant
  932.     ReDim idArray(1 To nCnt) As Integer
  933.     ReDim xypts(1 To nCnt * 2) As Double
  934.     
  935.     For i = 1 To nCnt
  936.         Select Case iRunMode
  937.         Case BYOBJECT
  938.         ' tell to drop referenced masters by master object.
  939.         Set marray(i) = mastersObj(i)
  940.         
  941.         Case BYINDEX
  942.         ' tell to drop masters designated by index.
  943.         marray(i) = i
  944.         
  945.         Case BYNAME
  946.         ' tell to drop masters designated by name.
  947.         marray(i) = mastersObj(i).Name
  948.         
  949.         Case BYUNIQUEID
  950.         ' tell to drop masters designated by uniqueid.
  951.         marray(i) = mastersObj(i).UniqueID
  952.         
  953.         End Select
  954.         
  955.         ' Set x components to 2,4,6,2,4,6,2,4,6,...
  956.         xypts(i * 2 - 1) = (((i - 1) Mod 3) + 1) * 2
  957.         
  958.         ' Set y components to 2,2,24,4,4,6,6,6,...
  959.         xypts(i * 2) = Int((i + 2) / 3) * 2
  960.     Next i
  961.  
  962.     nDropped = pagObj.DropMany(marray, xypts, idArray)
  963.     Debug.Print nDropped
  964.  
  965.     ' Exit the procedure bypassing the error handler
  966.     Exit Sub
  967.     
  968. errHandler:
  969.     ' Display a message to let the user know that we detected that
  970.     ' Visio was not running
  971.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  972.     
  973. End Sub
  974.  
  975. Sub ExtractCustomProperties()
  976. Attribute ExtractCustomProperties.VB_Description = "Demonstrates extracting the custom properties from a shape."
  977. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  978. ' Note: This example assumes that the VBA Samples stencil is open
  979. ' This example demonstrates:
  980. '   retrieving an instance of Visio
  981. '   retrieving the pages collection
  982. '   retrieving a page from the collection
  983. '   retrieving a named document from the documents collection
  984. '   dropping a shape
  985. '   setting a shape's text
  986. '   saving a document
  987.  
  988. Dim appVisio As Visio.Application ' Visio instance
  989. Dim stnObj As Visio.Document    ' Stencil that contains master
  990. Dim mastObj As Visio.Master     ' Master to drop
  991. Dim pagsObj As Visio.Pages      ' Pages collection of document
  992. Dim pagObj As Visio.Page        ' Page to work in
  993. Dim shpObj As Visio.Shape       ' Instance of master on page
  994. Dim celObj As Visio.Cell        ' Cell object for custom property
  995. Dim nRows As Integer            ' Number of rows in custom prop section
  996. Dim i As Integer                ' Loop variable
  997.     
  998.     ' Set an error handler
  999.     On Error GoTo errHandler
  1000.     
  1001.     ' Retrieve the current instance of Visio
  1002.     Set appVisio = GetObject(, "visio.application")
  1003.  
  1004.     ' Get the pages collection for the document
  1005.     ' Note the use of appVisio.ActiveDocument to refer to the current document
  1006.     Set pagsObj = appVisio.ActiveDocument.Pages
  1007.     
  1008.     ' Get a reference to the first page of the collection
  1009.     ' The code below uses the more typical short form of the code
  1010.     ' Set pagObj = pagsObj.Item(1)
  1011.     Set pagObj = pagsObj(1)
  1012.  
  1013.     ' Get the document object for the stencil
  1014.     ' The code below uses the more typical short form of the code
  1015.     ' Set stnObj = Documents.Item("VB Solutions.vss")
  1016.     Set stnObj = appVisio.Documents("VB Solutions.vss")
  1017.     
  1018.     ' Retrieve the master object for the Desktop PC shape
  1019.     ' The code below uses the more typical short form of the code
  1020.     ' Set mastObj = stnObj.Masters.Item("Desktop PC")
  1021.     Set mastObj = stnObj.Masters("Desktop PC")
  1022.  
  1023.     ' Drop the shape in the approximate middle of the page
  1024.     ' Coordinates passed with Drop are always in inches
  1025.     ' The Drop method returns a reference to the new shape object
  1026.     Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)
  1027.  
  1028.     ' This example demonstrates 2 methods of extracting custom properties
  1029.     ' First, we retrieve the value of a custom property by name
  1030.     ' Note that Prop.Computer implies Prop.Computer.Value
  1031.     Set celObj = shpObj.Cells("Prop.Computer")
  1032.     
  1033.     ' Now that we have the cell, get the value as a string and put it
  1034.     ' into the textbox on the form
  1035.     UserForm1.TextBox1.Text = celObj.ResultStr(Visio.visNone)
  1036.     
  1037.     ' Set the caption of the label so we know what we are looking at
  1038.     UserForm1.Label1.Caption = "Prop.Computer"
  1039.     
  1040.     ' Display the form
  1041.     UserForm1.Show
  1042.     
  1043.     ' The second method of accessing custom properties is using
  1044.     ' section, row, cell. This method is best when you want to iterate
  1045.     ' through all the properties
  1046.     nRows = shpObj.RowCount(Visio.visSectionProp)
  1047.     
  1048.     ' Make sure the list box is cleared
  1049.     UserForm2.ListBox1.Clear
  1050.     
  1051.     ' Loop through all the rows and add the value of Prop.Computer
  1052.     ' to the list box. Remember, Rows are numbered starting with 0.
  1053.     For i = 0 To nRows - 1
  1054.         Set celObj = shpObj.CellsSRC(Visio.visSectionProp, i, visCustPropsValue)
  1055.         UserForm2.ListBox1.AddItem celObj.LocalName & vbTab & celObj.ResultStr(Visio.visNone)
  1056.     Next i
  1057.     
  1058.     UserForm2.Show
  1059.  
  1060.     ' Exit the procedure bypassing the error handler
  1061.     Exit Sub
  1062.     
  1063. errHandler:
  1064.     ' Display a message to let the user know that we detected that
  1065.     ' Visio was not running
  1066.     MsgBox "Visio must be running and the stencil ""VB Solutions.vss"" must be open!", vbOKOnly, DVS_TITLE
  1067.     
  1068. End Sub
  1069.  
  1070. Sub GetAllDocNames()
  1071. Attribute GetAllDocNames.VB_Description = "Demonstrates retrieving and iterating through the Visio documents collection and accessing the name property."
  1072. ' Developing Visio Solutions, Chapter 11, "Using Visio Objects"
  1073. ' This example demonstrates:
  1074. '   retrieving an instance of Visio
  1075. '   retrieving the Visio documents collection
  1076. '   iterating through the document collection
  1077. '   retrieving the name property of a document
  1078.  
  1079. Dim appVisio As Visio.Application ' Visio instance
  1080. Dim docsObj As Visio.Documents  ' Documents collection
  1081. Dim docObj As Visio.Document    ' A Document
  1082. Dim curShapeIndx As Integer     ' Loop variable
  1083. Dim strDocName As String        ' String to hold name
  1084.     
  1085.     ' Set an error handler
  1086.     On Error GoTo errHandler
  1087.     
  1088.     ' Retrieve the current instance of Visio
  1089.     Set appVisio = GetObject(, "visio.application")
  1090.  
  1091.     'Get the Visio Documents collection
  1092.     Set docsObj = appVisio.Documents
  1093.  
  1094.     ' Make sure the listbox is cleared
  1095.     UserForm2.ListBox1.Clear
  1096.     
  1097.     ' Loop throught the documents collection
  1098.     For curShapeIndx = 1 To docsObj.Count
  1099.         ' Get the ith document from the Documents collection
  1100.         ' The code below uses the more typical short form of the code
  1101.         ' Set docObj = docsObj.Item(curShapeIndx)
  1102.         Set docObj = docsObj(curShapeIndx)
  1103.     
  1104.         ' Get the Document Name property
  1105.         strDocName = docObj.Name
  1106.     
  1107.         ' Add the document name to the form listbox
  1108.         UserForm2.ListBox1.AddItem strDocName
  1109.     Next curShapeIndx
  1110.     
  1111.     UserForm2.Show
  1112.     
  1113.     ' Exit the procedure bypassing the error handler
  1114.     Exit Sub
  1115.     
  1116. errHandler:
  1117.     ' Display a message to let the user know that we detected that
  1118.     ' Visio was not running
  1119.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  1120.     
  1121. End Sub
  1122.  
  1123. Sub GetDocName()
  1124. Attribute GetDocName.VB_Description = "Demonstrates retrieving a document from the document collection and accessing the name property."
  1125. ' Developing Visio Solutions, Chapter 11, "Using Visio Objects"
  1126. ' This example demonstrates:
  1127. '   retrieving an instance of Visio
  1128. '   retrieving a document from the collection
  1129. '   retrieving the name of a document
  1130.  
  1131. Dim appVisio As Visio.Application ' Visio instance
  1132. Dim docObj As Visio.Document    ' A Document
  1133. Dim strDocName As String        ' String to hold name
  1134.     
  1135.     ' Set an error handler
  1136.     On Error GoTo errHandler
  1137.     
  1138.     ' Retrieve the current instance of Visio
  1139.     Set appVisio = GetObject(, "visio.application")
  1140.  
  1141.     ' Get the first document from the Documents collection
  1142.     ' The code below uses the more typical short form of the code
  1143.     ' Set docObj = Documents.Item(1)
  1144.     Set docObj = appVisio.Documents(1)
  1145.  
  1146.     ' Get the Document Name property
  1147.     strDocName = docObj.Name
  1148.  
  1149.     ' Set the Text property of the text box to the document name
  1150.     UserForm1.TextBox1 = strDocName
  1151.     UserForm1.TextBox1.Enabled = False
  1152.     
  1153.     ' Set the label text and show the form
  1154.     UserForm1.Label1.Caption = "Document name:"
  1155.     UserForm1.Show
  1156.  
  1157.     ' Exit the procedure bypassing the error handler
  1158.     Exit Sub
  1159.     
  1160. errHandler:
  1161.     ' Display a message to let the user know that we detected that
  1162.     ' Visio was not running
  1163.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  1164.     
  1165. End Sub
  1166.  
  1167. Sub GetFormulaResults()
  1168. Attribute GetFormulaResults.VB_Description = "Demonstrates using the GetResults method to retrieve the results of multiple shape cell formulas in one call."
  1169. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  1170. ' This example demonstrates:
  1171. '   retrieving an instance of Visio
  1172. '   retrieving the current selection collection
  1173. '   retrieving the ID for a shape
  1174. '   using GetResults to retrieve the results of a formula for selected shapes
  1175.  
  1176. Dim appVisio As Visio.Application ' Visio instance
  1177. Dim selObj As Visio.Selection   ' Selection window
  1178. Dim i As Integer                ' Loop variable
  1179. Dim nCnt As Integer             ' Count of objects in selection collection
  1180.     
  1181.     ' Set an error handler
  1182.     On Error GoTo errHandler
  1183.     
  1184.     ' Retrieve the current instance of Visio
  1185.     Set appVisio = GetObject(, "visio.application")
  1186.  
  1187.     ' Get the selection collection for the active window
  1188.     Set selObj = appVisio.ActiveWindow.Selection
  1189.     
  1190.     ' retrieve the size of the collection
  1191.     nCnt = selObj.Count
  1192.     
  1193.     ' Raise an error condition. The error number for our purposes is unimportant!
  1194.     If nCnt = 0 Then Error.Raise vbObjectError + 1000
  1195.     
  1196.     ' Dimension an array to hold the returned results
  1197.     ReDim rgResults(1 To nCnt) As Variant
  1198.     
  1199.     ' Dimension an array to hold the inputs to GetResults
  1200.     ' Each entry requires 4 elements: shape ID, section, row, and cell
  1201.     ' Note if we invoked GetResults on a shape instead of on the page,
  1202.     ' each entry would only require 3 elements: section, row, and cell
  1203.     ReDim rgSRCStream(1 To nCnt * 4) As Integer
  1204.     
  1205.     ' Dimension an array to hold the unit specification for the desired
  1206.     ' results. If we had wanted all units the same this could have been
  1207.     ' a single element array initialized to the desired unit
  1208.     ReDim rgUnits(1 To nCnt) As Variant
  1209.     
  1210.     ' Initialize the units array. Let's get inches for odd numbered entries
  1211.     ' and mm for even
  1212.     
  1213.     For i = 1 To nCnt
  1214.         If i Mod 2 = 0 Then
  1215.             rgUnits(i) = "mm."
  1216.         Else
  1217.             rgUnits(i) = "in."
  1218.         End If
  1219.     Next i
  1220.     
  1221.     ' Initialize the inputs for each shape in the selection
  1222.     ' such that we retrieve the width of all the shapes
  1223.     For i = 0 To nCnt - 1
  1224.         ' Shape ID - this identifies the shape we're interested in
  1225.         rgSRCStream(4 * i + 1) = selObj(i + 1).ID
  1226.         
  1227.         ' Section - Width is part of the Object section
  1228.         rgSRCStream(4 * i + 2) = visSectionObject
  1229.         
  1230.         ' Row - Width is in the XFormOut row
  1231.         rgSRCStream(4 * i + 3) = visRowXFormOut
  1232.         
  1233.         ' Cell - the Width cell
  1234.         rgSRCStream(4 * i + 4) = visXFormWidth
  1235.     Next i
  1236.     
  1237.     ' Invoke the GetResults method on the page
  1238.     appVisio.ActivePage.GetResults rgSRCStream, 0, rgUnits, rgResults
  1239.     
  1240.     ' Make sure the listbox is cleared
  1241.     UserForm2.ListBox1.Clear
  1242.     
  1243.     ' Load the listbox with the formula for each shape
  1244.     For i = 1 To nCnt
  1245.         UserForm2.ListBox1.AddItem _
  1246.             "Sheet." & selObj(i).ID & ": " & rgResults(i - 1) & " " & rgUnits(i)
  1247.     Next i
  1248.     
  1249.     ' Display the listbox
  1250.     UserForm2.Caption = "Shape Widths (results):"
  1251.     UserForm2.Show
  1252.     
  1253.     ' Exit the procedure bypassing the error handler
  1254.     Exit Sub
  1255.     
  1256. errHandler:
  1257.     ' Display a message to let the user know that we detected that
  1258.     ' Visio was not running
  1259.     MsgBox "Visio must be running and shapes must be selected!", vbOKOnly, DVS_TITLE
  1260.  
  1261. End Sub
  1262.  
  1263. Sub GetSelectedShapes()
  1264. Attribute GetSelectedShapes.VB_Description = "Demonstrates retrieving the Selection collection for a window."
  1265. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes."
  1266. ' This example demonstrates:
  1267. '   retrieving an instance of Visio
  1268. '   retrieving the current selection collection
  1269. '   retrieving the name of each shape in the selection collection
  1270.  
  1271. Dim appVisio As Visio.Application ' Visio instance
  1272. Dim selObj As Visio.Selection
  1273. Dim shpObj As Visio.Shape
  1274. Dim curShpIndx As Integer
  1275.     
  1276.     ' Set an error handler
  1277.     On Error GoTo errHandler
  1278.     
  1279.     ' Retrieve the current instance of Visio
  1280.     Set appVisio = GetObject(, "visio.application")
  1281.  
  1282.     ' Retrieve the selection collection for the active window
  1283.     Set selObj = appVisio.ActiveWindow.Selection
  1284.     
  1285.     ' Raise an error condition. The error number for our purposes is unimportant!
  1286.     If selObj.Count = 0 Then Error.Raise vbObjectError + 1000
  1287.     
  1288.     ' Make sure the listbox is cleared
  1289.     UserForm2.ListBox1.Clear
  1290.     
  1291.     ' Loop through all the shapes in the selection
  1292.     For curShpIndx = 1 To selObj.Count
  1293.         ' Add the shapes name to the listbox
  1294.         UserForm2.ListBox1.AddItem selObj(curShpIndx).Name
  1295.     Next curShpIndx
  1296.     
  1297.     ' Display the listbox
  1298.     UserForm2.Show
  1299.     
  1300.     ' Exit the procedure bypassing the error handler
  1301.     Exit Sub
  1302.     
  1303. errHandler:
  1304.     ' Display a message to let the user know that we detected that
  1305.     ' Visio was not running
  1306.     MsgBox "Visio must be running and shapes must be selected!", vbOKOnly, DVS_TITLE
  1307.     
  1308. End Sub
  1309.  
  1310. Sub GetTheFormulas()
  1311. Attribute GetTheFormulas.VB_Description = "Demonstrates using the GetFormulas method to retrieve formulas from multiple shapes in one call."
  1312. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  1313. ' This example demonstrates:
  1314. '   retrieving an instance of Visio
  1315. '   retrieving the current selection collection
  1316. '   retrieving the ID for a shape
  1317. '   using GetFormulas to retrieve a formula for all the selected shapes
  1318.  
  1319. Dim appVisio As Visio.Application ' Visio instance
  1320. Dim selObj As Visio.Selection   ' Selection window
  1321. Dim i As Integer                ' Loop variable
  1322. Dim nCnt As Integer             ' Count of objects in selection collection
  1323.     
  1324.     ' Set an error handler
  1325.     On Error GoTo errHandler
  1326.     
  1327.     ' Retrieve the current instance of Visio
  1328.     Set appVisio = GetObject(, "visio.application")
  1329.  
  1330.     ' Get the selection collection for the active window
  1331.     Set selObj = appVisio.ActiveWindow.Selection
  1332.     
  1333.     ' retrieve the size of the collection
  1334.     nCnt = selObj.Count
  1335.     
  1336.     ' Raise an error condition. The error number for our purposes is unimportant!
  1337.     If nCnt = 0 Then Error.Raise vbObjectError + 1000
  1338.     
  1339.     ' Dimension an array to hold the returned formulas
  1340.     ReDim rgFormulas(1 To nCnt) As Variant
  1341.     
  1342.     ' Dimension an array to hold the inputs to GetFormulas
  1343.     ' Each entry requires 4 elements: shape ID, section, row, and cell
  1344.     ' Note if we invoked GetFormulas on a shape instead of on the page,
  1345.     ' each entry would only require 3 elements: section, row, and cell
  1346.     ReDim rgSRCStream(1 To nCnt * 4) As Integer
  1347.     
  1348.     ' Initialize the inputs for each shape in the selection
  1349.     ' such that we retrieve the width of all the shapes
  1350.     For i = 0 To nCnt - 1
  1351.         ' Shape ID - this identifies the shape we're interested in
  1352.         rgSRCStream(4 * i + 1) = selObj(i + 1).ID
  1353.         
  1354.         ' Section - Width is part of the Object section
  1355.         rgSRCStream(4 * i + 2) = visSectionObject
  1356.         
  1357.         ' Row - Width is in the XFormOut row
  1358.         rgSRCStream(4 * i + 3) = visRowXFormOut
  1359.         
  1360.         ' Cell - the Width cell
  1361.         rgSRCStream(4 * i + 4) = visXFormWidth
  1362.     Next i
  1363.     
  1364.     ' Invoke the GetFormulas method on the page
  1365.     appVisio.ActivePage.GetFormulas rgSRCStream, rgFormulas
  1366.     
  1367.     ' Make sure the listbox is cleared
  1368.     UserForm2.ListBox1.Clear
  1369.     
  1370.     ' Load the listbox with the formula for each shape
  1371.     For i = 1 To nCnt
  1372.         UserForm2.ListBox1.AddItem _
  1373.             "Sheet." & selObj(i).ID & ": " & rgFormulas(i - 1)
  1374.     Next i
  1375.     
  1376.     ' Display the listbox
  1377.     UserForm2.Caption = "Shape Widths:"
  1378.     UserForm2.Show
  1379.     
  1380.     ' Exit the procedure bypassing the error handler
  1381.     Exit Sub
  1382.     
  1383. errHandler:
  1384.     ' Display a message to let the user know that we detected that
  1385.     ' Visio was not running
  1386.     MsgBox "Visio must be running and shapes must be selected!", vbOKOnly, DVS_TITLE
  1387.     
  1388. End Sub
  1389.  
  1390. Sub GlueControlHandle()
  1391. Attribute GlueControlHandle.VB_Description = "Demonstrates gluing the control handle of a shape to the connection point of another shape."
  1392. ' Developing Visio Solutions, Chapter 12, "Creating Visio drawings from a program"
  1393. ' Note: This example assumes that the VBA Samples stencil is open
  1394. ' This example demonstrates:
  1395. '   retrieving an instance of Visio
  1396. '   retrieving a named document from the documents collection
  1397. '   dropping a shape
  1398. '   retrieving a cell object for a shape's control handle
  1399. '   gluing a control handle to a connection point
  1400.  
  1401. Dim appVisio As Visio.Application ' Visio instance
  1402. Dim stnObj As Visio.Document
  1403. Dim mastObj As Visio.Master
  1404. Dim shpObj1 As Visio.Shape
  1405. Dim shpObj2 As Visio.Shape
  1406. Dim celObj As Visio.Cell
  1407.     
  1408.     ' Set an error handler
  1409.     On Error GoTo errHandler
  1410.     
  1411.     ' Retrieve the current instance of Visio
  1412.     Set appVisio = GetObject(, "visio.application")
  1413.  
  1414.     ' Get the document object for the stencil
  1415.     ' The code below uses the more typical short form of the code
  1416.     ' Set stnObj = Documents.Item("VB Solutions.vss")
  1417.     Set stnObj = appVisio.Documents("VB Solutions.vss")
  1418.     
  1419.     ' Retrieve the master object for the Position shape
  1420.     ' The code below uses the more typical short form of the code
  1421.     ' Set mastObj = stnObj.Masters.Item("Position")
  1422.     Set mastObj = stnObj.Masters("Position")
  1423.  
  1424.     ' Drop the shape in the approximate middle of the page
  1425.     ' Coordinates passed with Drop are always in inches
  1426.     ' The Drop method returns a reference to the new shape object
  1427.     Set shpObj1 = appVisio.ActivePage.Drop(mastObj, 4.5, 5.5)
  1428.  
  1429.     ' Drop a second instance of the position master below and to
  1430.     ' the left of the first
  1431.     Set shpObj2 = appVisio.ActivePage.Drop(mastObj, 3.5, 4.5)
  1432.     
  1433.     ' retrieve the cell object for the control handle
  1434.     Set celObj = shpObj2.Cells("Controls.X1")
  1435.     
  1436.     ' glue the control handle to the bottom middle connection point
  1437.     celObj.GlueTo shpObj1.Cells("Connections.X4")
  1438.     
  1439.     ' Exit the procedure bypassing the error handler
  1440.     Exit Sub
  1441.     
  1442. errHandler:
  1443.     ' Display a message to let the user know that we detected that
  1444.     ' Visio was not running
  1445.     MsgBox "Visio must be running and the stencil ""VB Solutions.vss"" open!", vbOKOnly, DVS_TITLE
  1446.     
  1447. End Sub
  1448.  
  1449. Sub GlueSelectionHandle()
  1450. Attribute GlueSelectionHandle.VB_Description = "Demonstrates gluing a 1-D shape to the selection handle of another shape."
  1451. ' Developing Visio Solutions, Chapter 12, "Creating Visio drawings from a program"
  1452. ' Note: This example assumes that the VBA Samples stencil is open
  1453. ' This example demonstrates:
  1454. '   retrieving an instance of Visio
  1455. '   retrieving a named document from the documents collection
  1456. '   dropping a shape
  1457. '   retrieving a cell object for a shape's begin and end points
  1458. '   gluing a 1-D shape to a selection handle
  1459.  
  1460. Dim appVisio As Visio.Application ' Visio instance
  1461. Dim stnObj As Visio.Document
  1462. Dim mastObj As Visio.Master
  1463. Dim shpObj1 As Visio.Shape
  1464. Dim shpObj2 As Visio.Shape
  1465. Dim shpObj3 As Visio.Shape
  1466. Dim celObj As Visio.Cell
  1467.     
  1468.     ' Set an error handler
  1469.     On Error GoTo errHandler
  1470.     
  1471.     ' Retrieve the current instance of Visio
  1472.     Set appVisio = GetObject(, "visio.application")
  1473.  
  1474.     ' Get the document object for the stencil
  1475.     ' The code below uses the more typical short form of the code
  1476.     ' Set stnObj = Documents.Item("VB Solutions.vss")
  1477.     Set stnObj = appVisio.Documents("VB Solutions.vss")
  1478.     
  1479.     ' Retrieve the master object for the Cloud shape
  1480.     ' The code below uses the more typical short form of the code
  1481.     ' Set mastObj = stnObj.Masters.Item("Cloud")
  1482.     Set mastObj = stnObj.Masters("Cloud")
  1483.  
  1484.     ' Drop the shape in the approximate middle of the page
  1485.     ' Coordinates passed with Drop are always in inches
  1486.     ' The Drop method returns a reference to the new shape object
  1487.     Set shpObj1 = appVisio.ActivePage.Drop(mastObj, 5.5, 5.5)
  1488.     
  1489.     ' Reuse the mastObj variable to retrieve the Desktop PC master
  1490.     Set mastObj = stnObj.Masters("Desktop PC")
  1491.     
  1492.     ' Drop the shape onto the page
  1493.     Set shpObj2 = appVisio.ActivePage.Drop(mastObj, 3.5, 3.5)
  1494.     
  1495.     ' Reuse the mastObj variable to retrieve the comm-link master
  1496.     Set mastObj = stnObj.Masters("Comm-link")
  1497.     
  1498.     ' Drop the shape onto the page
  1499.     Set shpObj3 = appVisio.ActivePage.Drop(mastObj, 4, 4.5)
  1500.     
  1501.     ' retrieve the cell object for the BeginX of the comm-link
  1502.     Set celObj = shpObj3.Cells("BeginX")
  1503.     
  1504.     ' glue the begin point of the comm-link to the top selection handle
  1505.     ' of the Desktop PC
  1506.     celObj.GlueTo shpObj2.Cells("AlignTop")
  1507.     
  1508.     ' retrieve the cell object for the EndX of the comm-link
  1509.     Set celObj = shpObj3.Cells("EndX")
  1510.     
  1511.     ' glue the end point of the comm-link to the left selection handle
  1512.     ' of the cloud
  1513.     celObj.GlueTo shpObj1.Cells("AlignLeft")
  1514.     
  1515.     ' Exit the procedure bypassing the error handler
  1516.     Exit Sub
  1517.     
  1518. errHandler:
  1519.     ' Display a message to let the user know that we detected that
  1520.     ' Visio was not running
  1521.     MsgBox "Visio must be running and the stencil ""VB Solutions.vss"" open!", vbOKOnly, DVS_TITLE
  1522.     
  1523. End Sub
  1524.  
  1525. Sub GlueToGuide()
  1526. Attribute GlueToGuide.VB_Description = "Demonstrates gluing a shape to a guide."
  1527. ' Developing Visio Solutions, Chapter 12, "Creating Visio drawings from a program"
  1528. ' Note: This example assumes that the VBA Samples stencil is open
  1529. ' This example demonstrates:
  1530. '   retrieving an instance of Visio
  1531. '   retrieving a named document from the documents collection
  1532. '   dropping a shape
  1533. '   dropping a guide
  1534. '   retrieving a cell object for a shape's selection handle
  1535. '   gluing a shape to a guide
  1536.  
  1537. Dim appVisio As Visio.Application ' Visio instance
  1538. Dim stnObj As Visio.Document
  1539. Dim mastObj As Visio.Master
  1540. Dim shpObj1 As Visio.Shape
  1541. Dim shpObj2 As Visio.Shape
  1542. Dim celObj As Visio.Cell
  1543.     
  1544.     ' Set an error handler
  1545.     On Error GoTo errHandler
  1546.     
  1547.     ' Retrieve the current instance of Visio
  1548.     Set appVisio = GetObject(, "visio.application")
  1549.  
  1550.     ' Get the document object for the stencil
  1551.     ' The code below uses the more typical short form of the code
  1552.     ' Set stnObj = Documents.Item("VB Solutions.vss")
  1553.     Set stnObj = appVisio.Documents("VB Solutions.vss")
  1554.     
  1555.     ' Retrieve the master object for the Desktop PC shape
  1556.     ' The code below uses the more typical short form of the code
  1557.     ' Set mastObj = stnObj.Masters.Item("Desktop PC")
  1558.     Set mastObj = stnObj.Masters("Desktop PC")
  1559.  
  1560.     ' Drop the shape in the approximate middle of the page
  1561.     ' Coordinates passed with Drop are always in inches
  1562.     ' The Drop method returns a reference to the new shape object
  1563.     Set shpObj1 = appVisio.ActivePage.Drop(mastObj, 5.5, 5.5)
  1564.     
  1565.     ' Add a vertical guide to the page
  1566.     ' Note that the y coordinate is ignored for vertical guides
  1567.     Set shpObj2 = appVisio.ActivePage.AddGuide(visVert, 5, 0)
  1568.     
  1569.     ' retrieve the cell object for the right selection handle of the
  1570.     ' Desktop PC shape
  1571.     Set celObj = shpObj1.Cells("AlignLeft")
  1572.     
  1573.     ' glue the right selection handle of the Desktop PC to the guide
  1574.     celObj.GlueTo shpObj2.Cells("GuidePosY")
  1575.     'celObj.GlueTo shpObj2.Cells("PinY")
  1576.     
  1577.     ' Exit the procedure bypassing the error handler
  1578.     Exit Sub
  1579.     
  1580. errHandler:
  1581.     ' Display a message to let the user know that we detected that
  1582.     ' Visio was not running
  1583.     MsgBox "Visio must be running and the stencil ""VB Solutions.vss"" open!", vbOKOnly, DVS_TITLE
  1584.     
  1585. End Sub
  1586.  
  1587. Sub HelloWorld()
  1588. Attribute HelloWorld.VB_Description = "Demonstrates a number of fundamental Visio add-on development concepts."
  1589. ' Developing Visio Solutions, Chapter 11, "Using Visio Objects"
  1590. ' Note: This example assumes that the VBA Samples stencil is open
  1591. ' This example demonstrates:
  1592. '   retrieving an instance of Visio
  1593. '   retrieving the pages collection
  1594. '   retrieving a page from the collection
  1595. '   retrieving a named document from the documents collection
  1596. '   dropping a shape
  1597. '   setting a shape's text
  1598. '   saving a document
  1599.  
  1600. Dim appVisio As Visio.Application ' Visio instance
  1601. Dim docsObj As Visio.Documents  ' Visio documents collection
  1602. Dim docObj As Visio.Document    ' Visio drawing
  1603. Dim mastObj As Visio.Master     ' Master to drop
  1604. Dim pagObj As Visio.Page        ' Page to work in
  1605. Dim pagsObj As Visio.Pages      ' Pages collection of document
  1606. Dim shpObj As Visio.Shape       ' Instance of master on page
  1607. Dim stnObj As Visio.Document    ' Stencil that contains master
  1608.     
  1609.     ' Retrieve the current instance of Visio
  1610.     Set appVisio = CreateObject("visio.application")
  1611.  
  1612.     ' Retrieve the documents collection
  1613.     Set docsObj = appVisio.Documents
  1614.  
  1615.     ' Create a new document based on "VB Solutions.vst"
  1616.     Set docObj = docsObj.Add("VB Solutions.vst")
  1617.     
  1618.     ' Get the pages collection for the document
  1619.     ' Note the use of appVisio.ActiveDocument to refer to the current document
  1620.     Set pagsObj = appVisio.ActiveDocument.Pages
  1621.     
  1622.     ' Get a reference to the first page of the collection
  1623.     ' The code below uses the more typical short form of the code
  1624.     ' Set pagObj = pagsObj.Item(1)
  1625.     Set pagObj = pagsObj(1)
  1626.  
  1627.     ' Get the document object for the stencil
  1628.     ' The code below uses the more typical short form of the code
  1629.     ' Set stnObj = Documents.Item("VB Solutions.vss")
  1630.     Set stnObj = appVisio.Documents("VB Solutions.vss")
  1631.     
  1632.     ' Retrieve the master object for the Rectangle shape
  1633.     ' The code below uses the more typical short form of the code
  1634.     ' Set mastObj = stnObj.Masters.Item("Rectangle")
  1635.     Set mastObj = stnObj.Masters("Rectangle")
  1636.  
  1637.     ' Drop the shape in the approximate middle of the page
  1638.     ' Coordinates passed with Drop are always in inches
  1639.     ' The Drop method returns a reference to the new shape object
  1640.     Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)
  1641.     
  1642.     'Set the text of the shape
  1643.     shpObj.Text = "Hello World!"
  1644.  
  1645.     'Save the drawing.
  1646.     appVisio.ActiveDocument.SaveAs "hello.vsd"
  1647.     
  1648.     ' Display a message letting the user know we're done
  1649.     MsgBox "Drawing Finished!", vbOKOnly, "Hello World!"
  1650.  
  1651.     ' Quit Visio
  1652.     appVisio.Quit
  1653.  
  1654. End Sub
  1655.  
  1656. Sub IterateGeometry()
  1657. Attribute IterateGeometry.VB_Description = "Demonstrates retrieving the formulas for the geometry of a shape."
  1658. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes"
  1659. ' This example demonstrates:
  1660. '   retrieving an instance of Visio
  1661. '   retrieving the shapes collection for the ActivePage
  1662. '   retrieving the number of geometry sections for a shape
  1663. '   retrieving the number of rows in a section
  1664. '   retrieving the number of cells in a row
  1665. '   retrieving the local name for a cell
  1666. '   retrieving the formula for a cell
  1667.  
  1668. Dim appVisio As Visio.Application ' Visio instance
  1669. Dim shpObj As Visio.Shape       ' shape object to work on
  1670. Dim curGeomSect As Integer      ' Section number for accessing geometry section
  1671. Dim curGeomSectIndx As Integer  ' Loop variable for geometry sections
  1672. Dim nRows As Integer            ' number of rows in section
  1673. Dim nCells As Integer           ' number of cells in row
  1674. Dim curRow As Integer           ' current row number (0 based)
  1675. Dim curCell As Integer          ' current cell index (0 based)
  1676. Dim nSects As Integer           ' number of geometry sections in shape
  1677.     
  1678.     ' Set an error handler
  1679.     On Error GoTo errHandler
  1680.     
  1681.     ' Retrieve the current instance of Visio
  1682.     Set appVisio = GetObject(, "visio.application")
  1683.  
  1684.     ' Retrieve the first shape from the ActivePage
  1685.     ' The code below uses the more typical short form of the code
  1686.     ' Set shpObj = ActivePage.Shapes.Item(1)
  1687.     Set shpObj = appVisio.ActivePage.Shapes(1)
  1688.     
  1689.     ' Make sure the listbox is cleared
  1690.     UserForm2.ListBox1.Clear
  1691.  
  1692.     ' Get the count of gemetry section in the shape
  1693.     ' Note: If the shape is a group or a guide, this will be 0
  1694.     nSects = shpObj.GeometryCount
  1695.     
  1696.     ' Iterate through all geometry sections for the shape
  1697.     ' Since we are adding the current geometry section index to
  1698.     ' the constant visSectionFirstComponent, we must start with 0
  1699.     For curGeomSectIndx = 0 To nSects - 1
  1700.         ' Set a variable to use when accessing the current
  1701.         ' geometry section
  1702.         curGeomSect = visSectionFirstComponent + curGeomSectIndx
  1703.  
  1704.         ' Get the count of rows in the current geometry section
  1705.         nRows = shpObj.RowCount(curGeomSect)
  1706.  
  1707.         ' Loop through the rows. Remember the count is zero based
  1708.         For curRow = 0 To (nRows - 1)
  1709.             ' Get the count of cells in the current row
  1710.             nCells = shpObj.RowsCellCount(curGeomSect, curRow)
  1711.             
  1712.             ' Loop through the cells. Again this is zero based
  1713.             For curCell = 0 To (nCells - 1)
  1714.                 ' Retrieve the cell's formula and add to the listbox
  1715.                 UserForm2.ListBox1.AddItem _
  1716.                     shpObj.CellsSRC(curGeomSect, curRow, curCell).LocalName & _
  1717.                     ": " & shpObj.CellsSRC(curGeomSect, curRow, curCell).Formula
  1718.             Next curCell
  1719.         Next curRow
  1720.     Next curGeomSectIndx
  1721.     
  1722.     ' Display the list
  1723.     UserForm2.Show
  1724.     
  1725.     ' Exit the procedure bypassing the error handler
  1726.     Exit Sub
  1727.     
  1728. errHandler:
  1729.     ' Display a message to let the user know that we detected that
  1730.     ' Visio was not running
  1731.     MsgBox "Visio must be running and there must be shapes in the drawing!", vbOKOnly, DVS_TITLE
  1732.     
  1733. End Sub
  1734.  
  1735. Sub IteratePages()
  1736. Attribute IteratePages.VB_Description = "Demonstrates retrieving the pages for a document and determining whether a page is a foreground or background page."
  1737. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  1738. ' This example demonstrates:
  1739. '   retrieving an instance of Visio
  1740. '   retrieving the pages collection for a document
  1741. '   getting a page from the collection
  1742. '   checking whether a page is a background page
  1743.  
  1744. Dim appVisio As Visio.Application ' Visio instance
  1745. Dim pagsObj As Visio.Pages      ' Pages collection
  1746. Dim pagObj As Visio.Page        ' current page in collection
  1747. Dim i As Integer                ' current index into collection
  1748.     
  1749.     ' Set an error handler
  1750.     On Error GoTo errHandler
  1751.     
  1752.     ' Retrieve the current instance of Visio
  1753.     Set appVisio = GetObject(, "visio.application")
  1754.  
  1755.     ' retrieve the pages collection
  1756.     Set pagsObj = appVisio.ActiveDocument.Pages
  1757.  
  1758.     ' Make sure the listbox is cleared
  1759.     UserForm2.ListBox1.Clear
  1760.         
  1761.     ' iterate through the collection
  1762.     For i = 1 To pagsObj.Count
  1763.         ' retrieve the page object at the current index
  1764.         ' The code below uses the more typical short form of the code
  1765.         ' Set pagObj = pagsObj.Item(i)
  1766.         Set pagObj = pagsObj(i)
  1767.  
  1768.         ' Check whether the current page is a background page
  1769.         ' Display the name of all the foreground pages
  1770.         If pagObj.Background = False Then
  1771.             UserForm2.ListBox1.AddItem pagObj.Name
  1772.         End If
  1773.     Next i
  1774.     
  1775.     ' Display the listbox
  1776.     UserForm2.Show
  1777.  
  1778.     ' Exit the procedure bypassing the error handler
  1779.     Exit Sub
  1780.     
  1781. errHandler:
  1782.     ' Display a message to let the user know that we detected that
  1783.     ' Visio was not running
  1784.     MsgBox "Visio must be running and there must be at least one page in the drawing!", vbOKOnly, DVS_TITLE
  1785.     
  1786. End Sub
  1787.  
  1788. Sub ListLayers()
  1789. Attribute ListLayers.VB_Description = "Demonstrates retrieving the layers collection for a page and accessing the names of the layers."
  1790. ' Developing Visio Shapes, Chapter 14, "Working with Drawings and Shapes"
  1791. ' This example demonstrates:
  1792. '   retrieving an instance of Visio
  1793. '   retrieving the layers collection for a page
  1794. '   retrieving a layer from the layers collection
  1795. '   retrieving the name of a layer
  1796.  
  1797. Dim appVisio As Visio.Application ' Visio instance
  1798. Dim layersObj As Visio.Layers   ' layers collection
  1799. Dim layerObj As Visio.Layer     ' current layer from collection
  1800. Dim layerName As String         ' variable to hold name of layer
  1801. Dim i As Integer                ' current index into the collection
  1802.     
  1803.     ' Set an error handler
  1804.     On Error GoTo errHandler
  1805.     
  1806.     ' Retrieve the current instance of Visio
  1807.     Set appVisio = GetObject(, "visio.application")
  1808.  
  1809.     ' retrieve the layers collection for the active page
  1810.     Set layersObj = appVisio.ActivePage.Layers
  1811.     
  1812.     ' Make sure that the listbox is cleared
  1813.     UserForm2.ListBox1.Clear
  1814.     
  1815.     ' iterate through the layers collection
  1816.     For i = 1 To layersObj.Count
  1817.     
  1818.         ' retrieve the layer object at the current index
  1819.         ' The code below uses the more typical short form of the code
  1820.         ' Set layerObj = layersObj.Item(i)
  1821.         Set layerObj = layersObj(i)
  1822.         
  1823.         ' retrieve the name of the current layer
  1824.         layerName = layerObj.Name
  1825.         
  1826.         ' add the name to the listbox
  1827.         UserForm2.ListBox1.AddItem layerName
  1828.     Next i
  1829.  
  1830.     ' Display the listbox
  1831.     UserForm2.Show
  1832.     
  1833.     ' Exit the procedure bypassing the error handler
  1834.     Exit Sub
  1835.     
  1836. errHandler:
  1837.     ' Display a message to let the user know that we detected that
  1838.     ' Visio was not running
  1839.     MsgBox "Visio must be running and a drawing active!", vbOKOnly, DVS_TITLE
  1840.     
  1841. End Sub
  1842.  
  1843. Sub ListStyles()
  1844. Attribute ListStyles.VB_Description = "Demonstrates retrieving the styles collection and the names of the styles."
  1845. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes."
  1846. ' This example demonstrates:
  1847. '   retrieving an instance of Visio
  1848. '   retrieving the styles collection
  1849. '   retrieving the name of a style from the style object
  1850.  
  1851. Dim appVisio As Visio.Application ' Visio instance
  1852. Dim stylsObj As Visio.Styles    ' Styles collection
  1853. Dim stylObj As Visio.Style      ' current style from collection
  1854. Dim curStyleIndx As Integer     ' index into style collection
  1855. Dim styleName As String         ' name of current style
  1856.     
  1857.     ' Set an error handler
  1858.     On Error GoTo errHandler
  1859.     
  1860.     ' Retrieve the current instance of Visio
  1861.     Set appVisio = GetObject(, "visio.application")
  1862.  
  1863.     ' Retrieve the Styles collection of the current document
  1864.     ' Note the use of appVisio.ActiveDocument to get the current document
  1865.     Set stylsObj = appVisio.ActiveDocument.Styles
  1866.  
  1867.     ' Make sure the listbox is cleared
  1868.     UserForm2.ListBox1.Clear
  1869.     
  1870.     ' Iterate through the styles
  1871.     For curStyleIndx = 1 To stylsObj.Count
  1872.         ' get the object for the current style
  1873.         Set stylObj = stylsObj(curStyleIndx)
  1874.         
  1875.         ' Get the name property from the style object
  1876.         styleName = stylObj.Name
  1877.         
  1878.         ' Add the style name to the listbox
  1879.         UserForm2.ListBox1.AddItem styleName
  1880.     Next curStyleIndx
  1881.     
  1882.     ' Display the list
  1883.     UserForm2.Show
  1884.     
  1885.     ' Exit the procedure bypassing the error handler
  1886.     Exit Sub
  1887.     
  1888. errHandler:
  1889.     ' Display a message to let the user know that we detected that
  1890.     ' Visio was not running
  1891.     MsgBox "Visio must be running and a drawing active!", vbOKOnly, DVS_TITLE
  1892.     
  1893. End Sub
  1894.  
  1895. Sub LoadInterfaceFile()
  1896. Attribute LoadInterfaceFile.VB_Description = "Demonstrates loading a custom UI from a file."
  1897. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  1898. ' This example demonstrates:
  1899. '   retrieving an instance of Visio
  1900. '   loading a custom UI from a file
  1901.  
  1902. Dim appVisio As Visio.Application ' Visio instance
  1903. Dim uiObj As Visio.UIObject
  1904.     
  1905.     ' Retrieve the current instance of Visio
  1906.     Set appVisio = GetObject(, "visio.application")
  1907.  
  1908.     ' retrieve the UI object for the copy of the builtin menus
  1909.     Set uiObj = appVisio.BuiltInToolbars(Visio.visToolBarMSOffice)
  1910.     
  1911.     ' tell Visio to use the custom UI contained in the specified file
  1912.     uiObj.LoadFromFile "shortcut.vsu"
  1913.     appVisio.ActiveDocument.SetCustomToolbars uiObj
  1914.     
  1915.     ' Display a message box telling the user how to undo this change
  1916.     MsgBox "Custom menu set. Run the RestoreBuiltInUI macro to clear the custom toolbar.", _
  1917.         vbInformation, DVS_TITLE
  1918.  
  1919. End Sub
  1920.  
  1921. Sub NoVisioUI()
  1922. Attribute NoVisioUI.VB_Description = "Demonstrates hiding the Visio UI (menus, toolbars, and statusbars)."
  1923. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  1924. ' This example demonstrates:
  1925. '   retrieving an instance of Visio
  1926. '   retrieving the current state for Visio's statusbar, toolbar, and menus
  1927. '   turning of display of Visio's statusbar, toolbar, and menus
  1928.  
  1929. Dim appVisio As Visio.Application ' Visio instance
  1930. Dim toolbarState As Integer
  1931. Dim statusBarState As Integer
  1932. Dim menuState As Integer
  1933.     
  1934.     ' Set an error handler
  1935.     On Error GoTo errHandler
  1936.     
  1937.     ' Retrieve the current instance of Visio
  1938.     Set appVisio = GetObject(, "visio.application")
  1939.  
  1940.     ' Turn off the toolbar
  1941.     toolbarState = appVisio.ToolbarStyle
  1942.     appVisio.ToolbarStyle = Visio.VisToolbarFlavors.visToolBarNone
  1943.     
  1944.     ' Turn off the menu
  1945.     menuState = appVisio.ShowMenus
  1946.     appVisio.ShowMenus = False
  1947.     
  1948.     ' Turn off the statusbar
  1949.     statusBarState = appVisio.ShowStatusBar
  1950.     appVisio.ShowStatusBar = False
  1951.     
  1952.     ' Wait for the user
  1953.     MsgBox "Press OK to restore UI", vbOKOnly, DVS_TITLE
  1954.     
  1955.     ' Restore the UI
  1956.     appVisio.ShowMenus = menuState
  1957.     appVisio.ToolbarStyle = toolbarState
  1958.     appVisio.ShowStatusBar = statusBarState
  1959.     
  1960.     ' Exit the procedure bypassing the error handler
  1961.     Exit Sub
  1962.     
  1963. errHandler:
  1964.     ' Display a message to let the user know that we detected that
  1965.     ' Visio was not running
  1966.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  1967.     
  1968. End Sub
  1969.  
  1970. Sub RestoreBuiltInUI()
  1971. Attribute RestoreBuiltInUI.VB_Description = "Restores the builtin Visio UI."
  1972. 'Developing Visio Solutions, Chapter 16, "Customizing Visio's User Interface"
  1973. ' This example demonstrates
  1974. '   retrieving an instance of Visio
  1975. '   clearing a custom menu
  1976. '   clearing a custom toolbar
  1977.  
  1978. Dim appVisio As Visio.Application ' Visio instance
  1979.     
  1980.     ' Set an error handler
  1981.     On Error GoTo errHandler
  1982.     
  1983.     ' Retrieve the current instance of Visio
  1984.     Set appVisio = GetObject(, "visio.application")
  1985.  
  1986.     ' tell Visio to use the builtin menus
  1987.     appVisio.ActiveDocument.ClearCustomMenus
  1988.     
  1989.     ' tell Visio to use the builtin toolbars
  1990.     appVisio.ActiveDocument.ClearCustomToolbars
  1991.  
  1992.     ' Exit the procedure bypassing the error handler
  1993.     Exit Sub
  1994.     
  1995. errHandler:
  1996.     ' Display a message to let the user know that we detected that
  1997.     ' Visio was not running
  1998.     MsgBox "Visio must be running and a document active!", vbOKOnly, DVS_TITLE
  1999.     
  2000. End Sub
  2001.  
  2002. Sub SetTheFormulas()
  2003. Attribute SetTheFormulas.VB_Description = "Demonstrates using the SetFormulas method to set the formulas for multiple shapes using one call."
  2004. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  2005. ' This example demonstrates:
  2006. '   retrieving an instance of Visio
  2007. '   retrieving the current selection collection
  2008. '   retrieving the ID for a shape
  2009. '   using SetFormulas to set a formula for all the selected shapes
  2010.  
  2011. Dim appVisio As Visio.Application ' Visio instance
  2012. Dim selObj As Visio.Selection   ' Selection window
  2013. Dim i As Integer                ' Loop variable
  2014. Dim nCnt As Integer             ' Count of objects in selection collection
  2015. Dim nSet As Integer             ' Number of formulas successfully set
  2016.     
  2017.     ' Set an error handler
  2018.     On Error GoTo errHandler
  2019.     
  2020.     ' Retrieve the current instance of Visio
  2021.     Set appVisio = GetObject(, "visio.application")
  2022.  
  2023.     ' Get the selection collection for the active window
  2024.     Set selObj = appVisio.ActiveWindow.Selection
  2025.     
  2026.     ' retrieve the size of the collection
  2027.     nCnt = selObj.Count
  2028.     
  2029.     ' Dimension an array to hold the new formulas
  2030.     'ReDim rgFormulas(1 To nCnt) As Variant
  2031.     ReDim rgFormulas(1 To 1) As Variant
  2032.     
  2033.     rgFormulas(1) = "Height * 0.5"
  2034.     
  2035.     ' Dimension an array to hold the inputs to SetFormulas
  2036.     ' Each entry requires 4 elements: shape ID, section, row, and cell
  2037.     ' Note if we invoked SetFormulas on a shape instead of on the page,
  2038.     ' each entry would only require 3 elements: section, row, and cell
  2039.     ReDim rgSRCStream(1 To nCnt * 4) As Integer
  2040.     
  2041.     ' Initialize the inputs for each shape in the selection
  2042.     ' such that we set the width of all the shapes
  2043.     For i = 0 To nCnt - 1
  2044.         ' Shape ID - this identifies the shape we're interested in
  2045.         rgSRCStream(4 * i + 1) = selObj(i + 1).ID
  2046.         
  2047.         ' Section - Width is part of the Object section
  2048.         rgSRCStream(4 * i + 2) = visSectionObject
  2049.         
  2050.         ' Row - Width is in the XFormOut row
  2051.         rgSRCStream(4 * i + 3) = visRowXFormOut
  2052.         
  2053.         ' Cell - the Width cell
  2054.         rgSRCStream(4 * i + 4) = visXFormWidth
  2055.     Next i
  2056.     
  2057.     ' Invoke the SetFormulas method on the page
  2058.     ' Wrap this with an error handler since if the selection
  2059.     ' contains shapes with the width locked an exception will be thrown
  2060.     ' After the call we can tell if everything was set by checking the
  2061.     ' number of formulas set (returned by SetFormulas) against the number
  2062.     ' we sent down to be set. As an alternative, we could have added
  2063.     ' visSetBlastGuards to the flags parameter and that would set all the
  2064.     ' formulas ignoring whether they were guarded or not
  2065.     On Error Resume Next
  2066.     nSet = appVisio.ActivePage.SetFormulas(rgSRCStream, rgFormulas, visSetFormulas)
  2067.     On Error GoTo 0
  2068.     
  2069.     ' Put up a message if not all were set
  2070.     If (nSet <> nCnt) Then
  2071.         MsgBox "Only " & nSet & " of " & nCnt & "formulas were set!", vbOKOnly, DVS_TITLE
  2072.     End If
  2073.     
  2074.     ' Exit the procedure bypassing the error handler
  2075.     Exit Sub
  2076.     
  2077. errHandler:
  2078.     ' Display a message to let the user know that we detected that
  2079.     ' Visio was not running
  2080.     MsgBox "Visio must be running and one or more shapes selected!", vbOKOnly, DVS_TITLE
  2081.     
  2082. End Sub
  2083.  
  2084. Sub ShapeCounter()
  2085. Attribute ShapeCounter.VB_Description = "Demonstrates calling the ShapesCount function."
  2086. ' Developing Visio Solutions, Chapter 13, "Getting Information About Visio Drawings"
  2087. ' This example demonstrates:
  2088. '   retrieving an instance of Visio
  2089. '   using the ShapesCount function
  2090.  
  2091. Dim appVisio As Visio.Application ' Visio instance
  2092. Dim shpObj As Visio.Shape
  2093.     
  2094.     ' Set an error handler
  2095.     On Error GoTo errHandler
  2096.     
  2097.     ' Retrieve the current instance of Visio
  2098.     Set appVisio = GetObject(, "visio.application")
  2099.  
  2100.     ' retrieve the shape for the active page
  2101.     Set shpObj = appVisio.ActivePage.Shapes("ThePage")
  2102.     
  2103.     ' Display the number of shapes on the page
  2104.     MsgBox "ActivePage contains " & ShapesCount(shpObj) & " Shapes", _
  2105.         VBA.vbOKOnly, DVS_TITLE
  2106.  
  2107.     ' Exit the procedure bypassing the error handler
  2108.     Exit Sub
  2109.     
  2110. errHandler:
  2111.     ' Display a message to let the user know that we detected that
  2112.     ' Visio was not running
  2113.     MsgBox "Visio must be running and a drawing active!", vbOKOnly, DVS_TITLE
  2114.     
  2115. End Sub
  2116.  
  2117. Function ShapesCount(root As Visio.Shape) As Integer
  2118. Attribute ShapesCount.VB_Description = "Demonstrates retrieving the shapes collection, retrieving a shape from the collection, and determining the type of a shape."
  2119. ' Developing Visio Solutions, Chapter 13, "Getting Information About Visio Drawings"
  2120. ' This example demonstrates:
  2121. '   retrieving the shapes collection from a page or shape object
  2122. '   retrieving a shape from the collection
  2123. '   determining a shape's type
  2124.  
  2125. ' This example assumes that the Pages collection contains one or more groups.
  2126. ' The function counts shapes in groups, but does not count the groups that contain
  2127. ' the shapes (i.e. a group of 2 shapes returns 2 not 3 as it would if the group shape
  2128. ' were counted)
  2129.  
  2130. Dim shpsObj As Visio.Shapes     ' shapes collection
  2131. Dim shpObj As Visio.Shape       ' shape object
  2132. Dim i As Integer                ' loop variable
  2133. Dim iCount As Integer           ' return value
  2134.     
  2135.     ' Initialize the count to zero
  2136.     iCount = 0
  2137.     
  2138.     ' Retrieve the shapes collection from the root shape
  2139.     Set shpsObj = root.Shapes
  2140.     
  2141.     ' Loop through the shapes in the collection
  2142.     For i = 1 To shpsObj.Count
  2143.         ' Retrieve the current shape
  2144.         ' The code below uses the more typical short form of the code
  2145.         ' Set shpObj = shpsObj.Item(i)
  2146.         Set shpObj = shpsObj(i)
  2147.         
  2148.         ' Check if the current shape is a group. If it is, we must call
  2149.         ' this routine again (recursive call) to process the shapes within
  2150.         ' the group
  2151.         If shpObj.Type = visTypeGroup Then
  2152.             ' Set the count to the current count + the count returned
  2153.             ' from the processing of the group
  2154.             iCount = iCount + ShapesCount(shpObj)
  2155.         Else
  2156.             ' Increment the shape count
  2157.             iCount = iCount + 1
  2158.         End If
  2159.     Next
  2160.     
  2161.     ' Return the count of shapes starting at root
  2162.     ShapesCount = iCount
  2163.     
  2164. End Function
  2165.  
  2166. Sub ShowPageConnections()
  2167. Attribute ShowPageConnections.VB_Description = "Demonstrates retrieving the page connects collection and processing the FromSheet and ToSheet properties."
  2168. ' Developing Visio Solutions, Chapter 13, "Getting Information from Visio Drawings"
  2169. ' This example demonstrates:
  2170. '   retrieving an instance of Visio
  2171. '   retrieving the connects collection for the page
  2172. '   iterating through the collection
  2173. '   accessing the fromsheet and tosheet properties
  2174. '
  2175. ' This example assumes that the drawing contains glued shapes.
  2176.  
  2177. Dim appVisio As Visio.Application ' Visio instance
  2178. Dim pagsObj As Visio.Pages      ' page collection of document
  2179. Dim pagObj As Visio.Page        ' page to work on
  2180. Dim fromObj As Visio.Shape      ' object from connection connects to
  2181. Dim toObj As Visio.Shape        ' object to connection connects to
  2182. Dim consObj As Visio.Connects   ' Connects collection
  2183. Dim conObj As Visio.Connect     ' Connect object from collection
  2184. Dim curConnIndx As Integer      ' Loop variable for iterating through connections
  2185. Dim fromData As Integer         ' type of from connection
  2186. Dim fromStr As String           ' string to hold description of from connection
  2187. Dim toData As Integer           ' type of to connection
  2188. Dim toStr As String             ' string to hold description of to connection
  2189.     
  2190.     ' Set an error handler
  2191.     On Error GoTo errHandler
  2192.     
  2193.     ' Retrieve the current instance of Visio
  2194.     Set appVisio = GetObject(, "visio.application")
  2195.  
  2196.     ' Get the pages collection for the document
  2197.     ' Note the use of appVisio.ActiveDocument to refer to the current document
  2198.     Set pagsObj = appVisio.ActiveDocument.Pages
  2199.     
  2200.     ' Get a reference to the first page of the collection
  2201.     ' The code below uses the more typical short form of the code
  2202.     ' Set pagObj = pagsObj.Item(1)
  2203.     Set pagObj = pagsObj(1)
  2204.     
  2205.     ' Get the connects collection for the page
  2206.     Set consObj = pagObj.Connects
  2207.  
  2208.     ' Make sure the list box is emptied
  2209.     UserForm2.ListBox1.Clear
  2210.  
  2211.     ' Loop through the connects collection
  2212.     For curConnIndx = 1 To consObj.Count
  2213.         ' Get the current connect object from the collection
  2214.         Set conObj = consObj(curConnIndx)
  2215.         
  2216.         ' Get the From information
  2217.         Set fromObj = conObj.FromSheet
  2218.         fromData = conObj.FromPart
  2219.         
  2220.         ' Get the To information
  2221.         Set toObj = conObj.ToSheet
  2222.         toData = conObj.ToPart
  2223.  
  2224.         ' Use fromData to determine type of connection
  2225.         If fromData = visConnectError Then
  2226.             fromStr = "error"
  2227.         ElseIf fromData = visNone Then
  2228.             fromStr = "none"
  2229.         ElseIf fromData = visLeftEdge Then
  2230.             fromStr = "left"
  2231.         ElseIf fromData = visCenterEdge Then
  2232.             fromStr = "center"
  2233.         ElseIf fromData = visRightEdge Then
  2234.             fromStr = "right"
  2235.         ElseIf fromData = visBottomEdge Then
  2236.             fromStr = "bottom"
  2237.         ElseIf fromData = visMiddleEdge Then
  2238.             fromStr = "middle"
  2239.         ElseIf fromData = visTopEdge Then
  2240.             fromStr = "top"
  2241.         ElseIf fromData = visBeginX Then
  2242.             fromStr = "beginX"
  2243.         ElseIf fromData = visBeginY Then
  2244.             fromStr = "beginY"
  2245.         ElseIf fromData = visBegin Then
  2246.             fromStr = "begin"
  2247.         ElseIf fromData = visEndX Then
  2248.             fromStr = "endX"
  2249.         ElseIf fromData = visEndY Then
  2250.             fromStr = "endY"
  2251.         ElseIf fromData = visEnd Then
  2252.             fromStr = "end"
  2253.         ElseIf fromData >= visControlPoint Then
  2254.             fromStr = "controlPt_" & CStr(fromData - visControlPoint + 1)
  2255.         Else
  2256.             fromStr = "???"
  2257.         End If
  2258.  
  2259.         ' Use toData to determine what we connected to
  2260.         If toData = visConnectError Then
  2261.             toStr = "error"
  2262.         ElseIf toData = visNone Then
  2263.             toStr = "none"
  2264.         ElseIf toData = visGuideX Then
  2265.             toStr = "guideX"
  2266.         ElseIf toData = visGuideY Then
  2267.             toStr = "guideY"
  2268.         ElseIf toData >= visConnectionPoint Then
  2269.             toStr = "connectPt_" & CStr(toData - visConnectionPoint + 1)
  2270.         Else
  2271.             toStr = "???"
  2272.         End If
  2273.         
  2274.         ' Add the information to the list control
  2275.         UserForm2.ListBox1.AddItem _
  2276.             "from " & fromObj.Name & " " & fromStr & _
  2277.             " to " & toObj.Name & " " & toStr
  2278.         
  2279.     Next curConnIndx
  2280.  
  2281.     UserForm2.Show
  2282.     
  2283.     ' Exit the procedure bypassing the error handler
  2284.     Exit Sub
  2285.     
  2286. errHandler:
  2287.     ' Display a message to let the user know that we detected that
  2288.     ' Visio was not running
  2289.     MsgBox "Visio must be running and a drawing active!", vbOKOnly, DVS_TITLE
  2290.     
  2291. End Sub
  2292.  
  2293. Sub TurnOffGrid()
  2294. Attribute TurnOffGrid.VB_Description = "Turns off the drawing window grid."
  2295. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes."
  2296. ' This example demonstrates:
  2297. '   retrieving an instance of Visio
  2298. '   testing the window type
  2299. '   turning off the grid for a window
  2300.  
  2301. Dim appVisio As Visio.Application ' Visio instance
  2302.     
  2303.     ' Set an error handler
  2304.     On Error GoTo errHandler
  2305.     
  2306.     ' Retrieve the current instance of Visio
  2307.     Set appVisio = GetObject(, "visio.application")
  2308.  
  2309.     ' Check whether current window is a drawing window
  2310.     If appVisio.ActiveWindow.Type = visDrawing Then
  2311.         ' Turn off the grid
  2312.         appVisio.ActiveWindow.ShowGrid = False
  2313.     Else
  2314.         ' Tell the user why we're not turning off the grid
  2315.         MsgBox "Current window is not a drawing window.", vbOKOnly, DVS_TITLE
  2316.     End If
  2317.     
  2318.     ' Exit the procedure bypassing the error handler
  2319.     Exit Sub
  2320.     
  2321. errHandler:
  2322.     ' Display a message to let the user know that we detected that
  2323.     ' Visio was not running
  2324.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  2325.     
  2326. End Sub
  2327.  
  2328. Sub TurnOffRulers()
  2329. Attribute TurnOffRulers.VB_Description = "Turns off the drawing windows rulers."
  2330. ' Developing Visio Solutions, Chapter 14, "Working with Drawings and Shapes."
  2331. ' This example demonstrates:
  2332. '   retrieving an instance of Visio
  2333. '   testing the window type
  2334. '   turning off the rulers for a window
  2335.  
  2336. Dim appVisio As Visio.Application ' Visio instance
  2337.     
  2338.     ' Set an error handler
  2339.     On Error GoTo errHandler
  2340.     
  2341.     ' Retrieve the current instance of Visio
  2342.     Set appVisio = GetObject(, "visio.application")
  2343.  
  2344.     ' Check whether the current window is a drawing window
  2345.     If appVisio.ActiveWindow.Type = visDrawing Then
  2346.         ' Turn off the rulers
  2347.         appVisio.ActiveWindow.ShowRulers = False
  2348.     Else
  2349.         ' Tell the user why we're not turning off the rulers
  2350.         MsgBox "Current window is not a drawing window.", vbOKOnly, DVS_TITLE
  2351.     End If
  2352.     
  2353.     ' Exit the procedure bypassing the error handler
  2354.     Exit Sub
  2355.     
  2356. errHandler:
  2357.     ' Display a message to let the user know that we detected that
  2358.     ' Visio was not running
  2359.     MsgBox "Visio must be running!", vbOKOnly, DVS_TITLE
  2360.     
  2361. End Sub
  2362.